Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为类创建构造函数_Java_Constructor - Fatal编程技术网

Java 为类创建构造函数

Java 为类创建构造函数,java,constructor,Java,Constructor,我想弄明白这一点已经有一段时间了,似乎我不能在这门课上称鹦鹉为鹦鹉 class Bird { boolean f; parrot Bird[]; int x; } 我再也不能在类Bird中添加任何变量了,我已经尝试将它放入构造函数中,但它下面总是有一条红线 知道我做错了什么吗?这样改变你的课程: class Bird { boolean f; Bird[] parrot; int x; } (您更改了类型的顺序和属性的名称)您正在尝试创建自己的类(我想是因为您没有提供

我想弄明白这一点已经有一段时间了,似乎我不能在这门课上称鹦鹉为鹦鹉

class Bird {
boolean f;
parrot Bird[];
int x;
}
我再也不能在类Bird中添加任何变量了,我已经尝试将它放入构造函数中,但它下面总是有一条红线


知道我做错了什么吗?

这样改变你的课程:

class Bird {
    boolean f;
    Bird[] parrot;
    int x;
}

(您更改了类型的顺序和属性的名称)

您正在尝试创建自己的类(我想是因为您没有提供有关
客户机
类的完整信息)

然而,我认为问题在于
Bird
类中缺少构造函数。由于在
Bird
类中没有声明任何构造函数,Java将自动从
Object
中获取默认构造函数(这是包含所有其他内容的最大类)

要为Bird类创建构造函数,请执行以下操作:

class Animal {
    boolean f;
    Bird[ ] parrot;
    int x;

    public Animal() {
        this(false,inputParrot,inputX);
    }

    public Animal (boolean inputF, Bird[ ] inputParrot, int inputX) {
        f= inputF;
        parrot = new Bird[ ];
        parrot = inputParrot;
        x = inputX;
    }
}

我更改了您的类的名称,因为我假设您正在尝试创建
动物的树。因此,顺序将是,
动物
有一只
有一只
鹦鹉
(如果你不确定,研究has-a和is-a的关系)。

不,我不知道你做错了什么-因为你没有解释问题是什么。你想做什么?
鹦鹉【】此行错误,请交换单词。当您声明变量时,变量名称位于变量类型之后。例如,
Bird[]parrot
不是
parrot Bird[]