English 中文(简体)
我们可以创建一个接口的对象吗?
原标题:Can we create an object of an interface?
interface TestA {
    String toString();
}

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

结果是什么?

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.

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

问题回答

您在这里看到的是匿名内部类

给定以下接口:

interface Inter {
    public String getString();
}

您可以创建类似于它的实例的东西,如下所示:

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

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

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

这是一种非常常见的模式,用于Comparator接口作为闭包的模拟。

诀窍不仅在于匿名内部类,它还打印测试,因为它覆盖了toString方法,而System.out.println是一个Object,它隐式调用它的toString方法。

也试试这个。。。匿名类的名称已生成!

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

我们可以创建匿名类,实现接口:

匿名类使您的代码更加简洁。它们使您能够同时声明和实例化一个类。它们就像本地类,只是没有名称。如果只需要使用一次本地类,请使用它们。

如果您有一个接口,它声明了一个方法toString,那么您可以首先创建一个实现该接口的类,然后创建该类的对象:

interface TestA {
    String toString();
}

class TestB implements TestA {
    @Override
    public String toString() {
        return "test";
    }
}

public class Test {
    public static void main(String[] args) {
        System.out.println(new TestB());
    }
}

或者,您可以创建匿名类的对象来简化此代码:

interface TestA {
    String toString();
}

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

在这两种情况下,它都会打印“test”

我不知道这个问题的意义。如果这是一个面试问题,那么我可以说没关系。但在实时情况下,这不是实现继承的正确方法。因此,为了回答这个问题,您正在做的是一个匿名内部类

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

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

当然,结果将是测试





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签