Java 我们可以创建一个接口的对象吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4000062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 08:09:09  来源:igfitidea点击:

Can we create an object of an interface?

javainterfaceanonymous-class

提问by shirsendu shome

interface TestA {
    String toString();
}

public class Test {
    public static void main(String[] args) {
        System.out.println(new TestA() {
            public String toString() {
                return "test";
            }
        });
    }
}

What is the result?

结果是什么?

A. test
B. null
C. An exception is thrown at runtime .
D. Compilation fails because of an error in line 1.
E. Compilation fails because of an error in line 4.
F. Compilation fails because of an error in line 5.

A. 测试
B. null
C. 运行时抛出异常。
D. 第 1 行错误导致
编译失败。E. 第 4 行错误导致
编译失败。 F. 第 5 行错误导致编译失败。

What is the answer of this question and why? I have one more query regarding this question. In line 4 we are creating an object of A. Is it possible to create an object of an interface?

这个问题的答案是什么,为什么?关于这个问题,我还有一个疑问。在第 4 行中,我们正在创建 A 的对象。是否可以创建接口的对象?

回答by Stefan Kendall

testshould be the output. This is an example of an anonymous inner class.

test应该是输出。这是一个匿名内部类的例子。

This is a very common pattern used with the Comparatorinterface as an emulation of closures.

这是一个非常常见的模式,与Comparator接口一起用作闭包的模拟。

回答by jjnguy

What you are seeing here is an anonymous inner class:

你在这里看到的是一个匿名内部类

Given the following interface:

鉴于以下接口:

interface Inter {
    public String getString();
}

You can create something like an instance of it like so:

您可以像这样创建类似它的实例的东西:

Inter instance = new Inter() {
    @Override
    public String getString() { 
      return "HI"; 
    } 
  };

Now, you have an instance of the interface you defined. But, you should note that what you have actually done is defined a class that implements the interface and instantiated the class at the same time.

现在,您有一个您定义的接口的实例。但是,您应该注意,您实际上所做的是定义了一个实现接口并同时实例化该类的类。

回答by Marcos Vasconcelos

The trick is not exactly about the annonymous inner class, this prints test cause it overrides the toString method and while System.out.println a Object it implicit call it's toString method.

诀窍不完全是关于匿名内部类,这会打印测试,因为它覆盖了 toString 方法,而 System.out.println 是一个对象,它隐式调用它的 toString 方法。

回答by Kshitij

Try this too... The name of annonymous class is generated!

也试试这个... 匿名类的名字生成了!

Inter instance = new Inter() {
    public String getString(){ return "HI"+this.getClass(); }
};

回答by Abhijit Chakra

I don't know the significance of this question. If this is an interview question, then I can say it's okay. But in real time it's not the right approach to implement an inheritance.So coming to the answer of the question, here what you are doing is an anonymous inner class.

我不知道这个问题的意义。如果这是一个面试问题,那么我可以说没关系。但在实时,这不是实现继承的正确方法。所以来到问题的答案,这里你正在做的是一个匿名内部类

Here you are instantiating a classand implementing the inheritanceby writing,

在这里,您正在实例化一个并通过编写实现继承

System.out.println(new TestA() {
    public String toString() {
        return “test”;
    }
}); 

and ofcourse the result would be test

当然结果是 test