小编LR2*_*LR2的帖子

由于编译时异常,无法实例化子类

我是java的新手并且遇到了错误

包含...的封闭实例是必需的

当从与其父文件相同的源文件中定义的子类创建新对象时,但是当子类具有自己的源文件时,则不是这样,并且我想了解原因.

主文件:

package tessty
public class Tessty {
    public static void main(String[] args) {
        Person me = new Person();
        me.addtoitems();
    }
}
Run Code Online (Sandbox Code Playgroud)

"Person"类的另一个源文件:

package tessty;
import tessty.Item.*; // I included this import as per NetBeans' suggestions

public class Person {

    Item[] items;

    public Person() {
        items = new Item[3];
    }

    public void addtoitems() {
        items[0] = new Apple(); // Compile error thrown here
        items[1] = new Shoe(); // and here
        items[2] = new Hat(); // but not here …
Run Code Online (Sandbox Code Playgroud)

java file subclass

0
推荐指数
1
解决办法
234
查看次数

标签 统计

file ×1

java ×1

subclass ×1