This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                7年前关闭。
            
        

我有来自Java project helper的代码。

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


new IClasspathEntry[0]如何工作?




如何使用new实例化接口?
如何使用数组[0]代替括号()与new?


添加

我认为这是一种更安全的说法。

最佳答案

1.如何用new实例化一个接口?


不,接口永远不能实例化。


  2.如何使用数组[0]代替带有new的括号()?


IClasspathEntry[0]只是数组中索引为零的IClasspathEntry(well, asubtype of IClasspathEntry)类型的元素。您无法实例化接口。

IClasspathEntry[] arr = new IClasspathEntry[size];


上面只是创建了一个IClasspathEntry类型的数组,该数组接受subtypes(class's which implement 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;
    }
}

10-05 19:53