java - "new IClasspathEntry[0]"是如何工作的?

标签 java interface new-operator

我有来自 Java project helper 的代码.

javaProject.setRawClasspath(new IClasspathEntry[0], null);

如何new IClasspathEntry[0]有效吗?

enter image description here

  1. 如何使用 new 实例化接口(interface)?
  2. 数组如何 [0]使用 () 代替括号有新的吗?

添加

我认为这是一种更安全的表示 null 的方式。

最佳答案

1.How an interface is instantiated with new?

不,接口(interface)永远不能被实例化。

2.How an array [0] is used instead of parenthesis () with new?

IClasspathEntry[0] 只是数组中索引零处的 IClasspathEntry(好吧,IClasspathEntry 的子类型) 类型的元素。您无法实例化接口(interface)。

IClasspathEntry[] arr = new IClasspathEntry[size];

上面只是创建了一个IClasspathEntry类型的数组,它接受subtypes(实现IClasspathEntry的类)元素。

示例代码:

interface IClasspathEntry {}

class Xyz implements IClasspathEntry {}

class main
{
    public static void main(String...args)
    {
        IClasspathEntry[] arr = new IClasspathEntry[1];
        IClasspathEntry inst = new Xyz();
        arr[0] = inst;
    }
}

关于java - "new IClasspathEntry[0]"是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13941588/

相关文章:

java arraylist添加时出现空指针异常

c++ - 基本的新建/删除运算符(operator)日志记录

javax.validation.ConstraintDeclarationException : HV000131. 分层对象中的级联验证

java - 调用 notifyDataSetChanged 后 Android ListView 未更新

dataframe - 修改go中Stringer接口(interface)中的一个默认值

python - Zope 接口(interface)的目的?

c# - 接口(interface)和共享实现

c++ - 隐式的 new 和 delete 运算符杀死性能

java - Jar 文件初始化程序异常

java - 哪些 Eclipse IDE 支持 JSF 2.0?