【发布时间】:2019-08-11 05:38:56
【问题描述】:
在 Java 中,我们使用接口对用户隐藏实现。接口只包含抽象方法,因为抽象方法没有主体,我们无法在没有构造函数的情况下创建对象。像这样的
public interface ExampleInterface {
}
public class Example implements ExampleInterface {
private static void main(String[] args) {
// This is not possible
ExampleInterface objI = new ExampleInterface();
// However this is
ExampleInterface[] arrI = new ExampleInterface[10];
}
}
我的问题是为什么 Java 允许创建对象接口数组以及有人可能在哪里使用它?使用它是一种很好的编码习惯,或者最好创建一个实现接口的具体类,然后创建该类的数组。
【问题讨论】: