我有来自 Java project helper 的代码.
javaProject.setRawClasspath(new IClasspathEntry[0], null);
如何new IClasspathEntry[0]
有效吗?
- 如何使用 new 实例化接口(interface)?
- 数组如何
[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/