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

时间:2021-08-23 00:19:06
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的对象。是否可以创建接口的对象?

5 个解决方案

#1


81  

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.

现在,您有一个您定义的接口实例。但是,您应该注意到您实际执行的操作是定义了一个实现接口并同时实例化该类的类。

#2


5  

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

测试应该是输出。这是一个匿名内部类的示例。

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

这是与Comparator接口一起使用的非常常见的模式,作为闭包的仿真。

#3


1  

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是一个Object,它隐式调用它的toString方法。

#4


1  

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

试试这个......生成匿名类的名称!

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

#5


-1  

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 class and implementing the inheritance by writing,

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

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

and ofcourse the result would be test

当然,结果将是测试

#1


81  

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.

现在,您有一个您定义的接口实例。但是,您应该注意到您实际执行的操作是定义了一个实现接口并同时实例化该类的类。

#2


5  

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

测试应该是输出。这是一个匿名内部类的示例。

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

这是与Comparator接口一起使用的非常常见的模式,作为闭包的仿真。

#3


1  

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是一个Object,它隐式调用它的toString方法。

#4


1  

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

试试这个......生成匿名类的名称!

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

#5


-1  

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 class and implementing the inheritance by writing,

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

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

and ofcourse the result would be test

当然,结果将是测试