Java构造函数继承。构造函数未定义。

huangapple go评论68阅读模式
英文:

Java Constructor Inheritance. Constructor is undefined

问题

以下是您要翻译的部分:

在一个项目中,我需要创建一个扩展另一个类的类,并且以下是我所创建的该类的构造函数。在尝试在我的主要方法类中创建一个方法时,我遇到了一个错误,错误消息是:“尝试创建主方法类中的方法时,构造函数Monkey(String, String, String, String, String, String, String, String, String, String, String, boolean, String)未定义”。

我的Monkey类中的构造函数如下:

public Monkey(String name, String gender, String age, String weight, String tailLength, String height, String bodyLength, String species, String acquisitionDate, String acquisitionCountry, boolean reserved, String inServiceCountry) {
    setName(name);
    setGender(gender);
    setAge(age);
    setWeight(weight);
    setTailLength(tailLength);
    setHeight(height);
    setBodyLength(bodyLength);
    setSpecies(species);
    setAcquisitionDate(acquisitionDate);
    setAcquisitionLocation(acquisitionCountry);
    setReserved(reserved);
    setInServiceCountry(inServiceCountry);
}

我的主要类中的方法如下:

public static void initializeMonkeyList() {
    Monkey monkey1 = new Monkey("Steve", "male", "24", "15.4", "12", "20", "16", "capuchin", "04-26-1999", "United States", false, "United States");
    monkeyList.add(monkey1);
}
英文:

I am working on a project where I needed to create a class that extends another class and listed below is my constructor for said class. I am getting an error which is "The constructor Monkey(String, String, String, String, String, String, String, String, String, String, String, boolean, String) is undefined" when trying to create a method in my primary method class.

My monkey constructor in my monkey class

public Monkey(String name, String gender, String age, String weight, String tailLength, String height, String bodyLength, String species, String acquisitionDate, String aquisitionCountry, boolean reserved, String inServiceCountry) {
        setName(name);
	setGender(gender);
	setAge(age);
	setWeight(weight);
	setTailLength(tailLength);
	setHeight(height);
	setBodyLength(bodyLength);
	setSpecies(species);
	setAcquisitionDate(acquisitionDate);
	setAcquisitionLocation(acquisitionCountry);
	setTrainingStatus(trainingStatus);
	setReserved(reserved);
	setInServiceCountry(inServiceCountry);

The method in my primary class

public static void initializeMonkeyList() {
    	Monkey monkey1 = new Monkey("Steve", "male", "24", "15.4", "12", "20", "16", "capuchin", "04-26-1999", "United States", "intake", false, "United States");
        monkeyList.add(monkey1);
}

答案1

得分: 4

You have some issues in your code:

  • There is a typo in “acquisitionCountry” and it should be “acquisitionLocation”. The corrected line should be setAcquisitionLocation(acquisitionLocation);
  • The constructor is missing the “trainingStatus” parameter.
  • The “intake” value is in the wrong place when calling the constructor.

Here’s the corrected version:

import java.util.ArrayList;
import java.util.List;

class Scratch {
    static List<Monkey> monkeyList = new ArrayList<>();

    public static void main(String[] args) {
        initializeMonkeyList();
        System.out.println(monkeyList);
    }

    public static void initializeMonkeyList() {
        Monkey monkey1 = new Monkey("Steve", "male", "24", "15.4", "12", "20", "16", "capuchin", "04-26-1999", "United States", false, "United States", "intake");
        monkeyList.add(monkey1);
    }

    public static class Monkey {

        String name, gender, age, weight, tailLength, height, bodyLength, species, acquisitionDate, acquisitionLocation, inServiceCountry, trainingStatus;
        boolean reserved;

        public Monkey(String name, String gender, String age, String weight, String tailLength, String height, String bodyLength, String species, String acquisitionDate, String acquisitionLocation, boolean reserved, String inServiceCountry, String trainingStatus) {
            setName(name);
            setGender(gender);
            setAge(age);
            setWeight(weight);
            setTailLength(tailLength);
            setHeight(height);
            setBodyLength(bodyLength);
            setSpecies(species);
            setAcquisitionDate(acquisitionDate);
            setAcquisitionLocation(acquisitionLocation);
            setTrainingStatus(trainingStatus);
            setReserved(reserved);
            setInServiceCountry(inServiceCountry);
        }

        // Rest of your code...
    }
}

Output:

[Monkey{name='Steve', gender='male', age='24', weight='15.4', tailLength='12', height='20', bodyLength='16', species='capuchin', acquisitionDate='04-26-1999', acquisitionLocation='United States', inServiceCountry='United States', trainingStatus='intake', reserved=false}]

It is not considered good practice to have a large number of arguments. As suggested by others, you could implement the builder pattern.

Here is an example implementation using the builder pattern:

import java.util.ArrayList;
import java.util.List;

class Scratch {
    static List<Monkey> monkeyList = new ArrayList<>();

    public static void main(String[] args) {
        initializeMonkeyList();
        System.out.println(monkeyList);
    }

    public static void initializeMonkeyList() {
        Monkey monkey1 = new Monkey.Builder()
                .name("Steve")
                .gender("male")
                .age("24")
                .weight("15.4")
                .tailLength("12")
                .height("20")
                .bodyLength("16")
                .species("capuchin")
                .acquisitionDate("04-26-1999")
                .acquisitionLocation("United States")
                .reserved(false)
                .inServiceCountry("United States")
                .trainingStatus("intake")
                .build();
        monkeyList.add(monkey1);
    }

    public static class Monkey {
        private String name, gender, age, weight, tailLength, height, bodyLength, species, acquisitionDate, acquisitionLocation, inServiceCountry, trainingStatus;
        private boolean reserved;

        private Monkey(Builder builder) {
            this.name = builder.name;
            this.gender = builder.gender;
            this.age = builder.age;
            this.weight = builder.weight;
            this.tailLength = builder.tailLength;
            this.height = builder.height;
            this.bodyLength = builder.bodyLength;
            this.species = builder.species;
            this.acquisitionDate = builder.acquisitionDate;
            this.acquisitionLocation = builder.acquisitionLocation;
            this.inServiceCountry = builder.inServiceCountry;
            this.trainingStatus = builder.trainingStatus;
            this.reserved = builder.reserved;
        }

        public static class Builder {
            private String name, gender, age, weight, tailLength, height, bodyLength, species, acquisitionDate, acquisitionLocation, inServiceCountry, trainingStatus;
            private boolean reserved;

            public Builder name(String name) {
                this.name = name;
                return this;
            }

            public Builder gender(String gender) {
                this.gender = gender;
                return this;
            }

            public Builder age(String age) {
                this.age = age;
                return this;
            }

            public Builder weight(String weight) {
                this.weight = weight;
                return this;
            }

            public Builder tailLength(String tailLength) {
                this.tailLength = tailLength;
                return this;
            }

            public Builder height(String height) {
                this.height = height;
                return this;
            }

            public Builder bodyLength(String bodyLength) {
                this.bodyLength = bodyLength;
                return this;
            }

            public Builder species(String species) {
                this.species = species;
                return this;
            }

            public Builder acquisitionDate(String acquisitionDate) {
                this.acquisitionDate = acquisitionDate;
                return this;
            }

            public Builder acquisitionLocation(String acquisitionLocation) {
                this.acquisitionLocation = acquisitionLocation;
                return this;
            }

            public Builder inServiceCountry(String inServiceCountry) {
                this.inServiceCountry = inServiceCountry;
                return this;
            }

            public Builder trainingStatus(String trainingStatus) {
                this.trainingStatus = trainingStatus;
                return this;
            }

            public Builder reserved(boolean reserved) {
                this.reserved = reserved;
                return this;
            }

            public Monkey build() {
                return new Monkey(this);
            }
        }

        // Rest of your code...
    }
}
英文:

You have some issues in your code:

  • There is a typo in “acquisitionCountry” and it should be “acquisitionLocation”. The corrected line should be setAcquisitionLocation(acquisitionLocation);
  • The constructor is missing the “trainingStatus” parameter.
  • The “intake” value is in the wrong place when calling the constructor.

Here’s the corrected version:

import java.util.ArrayList;
import java.util.List;
class Scratch {
static List&lt;Monkey&gt; monkeyList = new ArrayList&lt;&gt;();
public static void main(String[] args) {
initializeMonkeyList();
System.out.println(monkeyList);
}
public static void initializeMonkeyList() {
Monkey monkey1 = new Monkey(&quot;Steve&quot;, &quot;male&quot;, &quot;24&quot;, &quot;15.4&quot;, &quot;12&quot;, &quot;20&quot;, &quot;16&quot;, &quot;capuchin&quot;, &quot;04-26-1999&quot;, &quot;United States&quot;,  false, &quot;United States&quot;, &quot;intake&quot;);
monkeyList.add(monkey1);
}
public static class Monkey {
String name,  gender,  age,  weight,  tailLength,  height,  bodyLength,  species,  acquisitionDate, acquisitionCountry, inServiceCountry, trainingStatus;
boolean reserved;
public Monkey(String name, String gender, String age, String weight, String tailLength, String height, String bodyLength, String species, String acquisitionDate, String acquisitionCountry, boolean reserved, String inServiceCountry, String trainingStatus) {
setName(name);
setGender(gender);
setAge(age);
setWeight(weight);
setTailLength(tailLength);
setHeight(height);
setBodyLength(bodyLength);
setSpecies(species);
setAcquisitionDate(acquisitionDate);
setAcquisitionCountry(acquisitionCountry);
setTrainingStatus(trainingStatus);
setReserved(reserved);
setInServiceCountry(inServiceCountry);
}
@Override
public String toString() {
return &quot;Monkey{&quot; +
&quot;name=&#39;&quot; + name + &#39;\&#39;&#39; +
&quot;, gender=&#39;&quot; + gender + &#39;\&#39;&#39; +
&quot;, age=&#39;&quot; + age + &#39;\&#39;&#39; +
&quot;, weight=&#39;&quot; + weight + &#39;\&#39;&#39; +
&quot;, tailLength=&#39;&quot; + tailLength + &#39;\&#39;&#39; +
&quot;, height=&#39;&quot; + height + &#39;\&#39;&#39; +
&quot;, bodyLength=&#39;&quot; + bodyLength + &#39;\&#39;&#39; +
&quot;, species=&#39;&quot; + species + &#39;\&#39;&#39; +
&quot;, acquisitionDate=&#39;&quot; + acquisitionDate + &#39;\&#39;&#39; +
&quot;, acquisitionCountry=&#39;&quot; + acquisitionCountry + &#39;\&#39;&#39; +
&quot;, inServiceCountry=&#39;&quot; + inServiceCountry + &#39;\&#39;&#39; +
&quot;, trainingStatus=&#39;&quot; + trainingStatus + &#39;\&#39;&#39; +
&quot;, reserved=&quot; + reserved +
&#39;}&#39;;
}
public void setTrainingStatus(String trainingStatus) {
this.trainingStatus = trainingStatus;
}
public void setName(String name) {
this.name = name;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setAge(String age) {
this.age = age;
}
public void setWeight(String weight) {
this.weight = weight;
}
public void setTailLength(String tailLength) {
this.tailLength = tailLength;
}
public void setHeight(String height) {
this.height = height;
}
public void setBodyLength(String bodyLength) {
this.bodyLength = bodyLength;
}
public void setSpecies(String species) {
this.species = species;
}
public void setAcquisitionDate(String acquisitionDate) {
this.acquisitionDate = acquisitionDate;
}
public void setAcquisitionCountry(String acquisitionCountry) {
this.acquisitionCountry = acquisitionCountry;
}
public void setInServiceCountry(String inServiceCountry) {
this.inServiceCountry = inServiceCountry;
}
public void setReserved(boolean reserved) {
this.reserved = reserved;
}
}
}

Output:

[Monkey{name=&#39;Steve&#39;, gender=&#39;male&#39;, age=&#39;24&#39;, weight=&#39;15.4&#39;, tailLength=&#39;12&#39;, height=&#39;20&#39;, bodyLength=&#39;16&#39;, species=&#39;capuchin&#39;, acquisitionDate=&#39;04-26-1999&#39;, acquisitionCountry=&#39;United States&#39;, inServiceCountry=&#39;United States&#39;, trainingStatus=&#39;intake&#39;, reserved=false}]

It is not considered good practice to have a large number of arguments. As suggested by others, you could implement the builder pattern.

Here is an example implementation using the builder pattern:

import java.util.ArrayList;
import java.util.List;
class Scratch {
static List&lt;Monkey&gt; monkeyList = new ArrayList&lt;&gt;();
public static void main(String[] args) {
initializeMonkeyList();
System.out.println(monkeyList);
}
public static void initializeMonkeyList() {
Monkey monkey1 = new Monkey.Builder()
.name(&quot;Steve&quot;)
.gender(&quot;male&quot;)
.age(&quot;24&quot;)
.weight(&quot;15.4&quot;)
.tailLength(&quot;12&quot;)
.height(&quot;20&quot;)
.bodyLength(&quot;16&quot;)
.species(&quot;capuchin&quot;)
.acquisitionDate(&quot;04-26-1999&quot;)
.acquisitionCountry(&quot;United States&quot;)
.reserved(false)
.inServiceCountry(&quot;United States&quot;)
.trainingStatus(&quot;intake&quot;)
.build();
monkeyList.add(monkey1);
}
public static class Monkey {
private String name, gender, age, weight, tailLength, height, bodyLength, species, acquisitionDate, acquisitionCountry, inServiceCountry, trainingStatus;
private boolean reserved;
private Monkey(Builder builder) {
this.name = builder.name;
this.gender = builder.gender;
this.age = builder.age;
this.weight = builder.weight;
this.tailLength = builder.tailLength;
this.height = builder.height;
this.bodyLength = builder.bodyLength;
this.species = builder.species;
this.acquisitionDate = builder.acquisitionDate;
this.acquisitionCountry = builder.acquisitionCountry;
this.inServiceCountry = builder.inServiceCountry;
this.trainingStatus = builder.trainingStatus;
this.reserved = builder.reserved;
}
public static class Builder {
private String name, gender, age, weight, tailLength, height, bodyLength, species, acquisitionDate, acquisitionCountry, inServiceCountry, trainingStatus;
private boolean reserved;
public Builder name(String name) {
this.name = name;
return this;
}
public Builder gender(String gender) {
this.gender = gender;
return this;
}
public Builder age(String age) {
this.age = age;
return this;
}
public Builder weight(String weight) {
this.weight = weight;
return this;
}
public Builder tailLength(String tailLength) {
this.tailLength = tailLength;
return this;
}
public Builder height(String height) {
this.height = height;
return this;
}
public Builder bodyLength(String bodyLength) {
this.bodyLength = bodyLength;
return this;
}
public Builder species(String species) {
this.species = species;
return this;
}
public Builder acquisitionDate(String acquisitionDate) {
this.acquisitionDate = acquisitionDate;
return this;
}
public Builder acquisitionCountry(String acquisitionCountry) {
this.acquisitionCountry = acquisitionCountry;
return this;
}
public Builder inServiceCountry(String inServiceCountry) {
this.inServiceCountry = inServiceCountry;
return this;
}
public Builder trainingStatus(String trainingStatus) {
this.trainingStatus = trainingStatus;
return this;
}
public Builder reserved(boolean reserved) {
this.reserved = reserved;
return this;
}
public Monkey build() {
return new Monkey(this);
}
}
@Override
public String toString() {
return &quot;Monkey{&quot; +
&quot;name=&#39;&quot; + name + &#39;\&#39;&#39; +
&quot;, gender=&#39;&quot; + gender + &#39;\&#39;&#39; +
&quot;, age=&#39;&quot; + age + &#39;\&#39;&#39; +
&quot;, weight=&#39;&quot; + weight + &#39;\&#39;&#39; +
&quot;, tailLength=&#39;&quot; + tailLength + &#39;\&#39;&#39; +
&quot;, height=&#39;&quot; + height + &#39;\&#39;&#39; +
&quot;, bodyLength=&#39;&quot; + bodyLength + &#39;\&#39;&#39; +
&quot;, species=&#39;&quot; + species + &#39;\&#39;&#39; +
&quot;, acquisitionDate=&#39;&quot; + acquisitionDate + &#39;\&#39;&#39; +
&quot;, acquisitionCountry=&#39;&quot; + acquisitionCountry + &#39;\&#39;&#39; +
&quot;, inServiceCountry=&#39;&quot; + inServiceCountry + &#39;\&#39;&#39; +
&quot;, trainingStatus=&#39;&quot; + trainingStatus + &#39;\&#39;&#39; +
&quot;, reserved=&quot; + reserved +
&#39;}&#39;;
}
}
}

答案2

得分: -2

你仍然缺少一个参数。

name                    "Steve"
gender                  "male"
age                     "24"
weight                  "15.4"
tailLength              "12"
height                  "20"
bodyLength              "16"
species                 "capuchin"
acquisitionDate         "04-26-1999"
aquisitionCountry       "United States"
reserved                "intake"
inServiceCountry        false
"United States"
英文:

You're still missing an argument.

name                    &quot;Steve&quot;
gender                  &quot;male&quot;
age                     &quot;24&quot;
weight                  &quot;15.4&quot;
tailLength              &quot;12&quot;
height                  &quot;20&quot;
bodyLength              &quot;16&quot;
species                 &quot;capuchin&quot;
acquisitionDate         &quot;04-26-1999&quot;
aquisitionCountry       &quot;United States&quot;
reserved                &quot;intake&quot;
inServiceCountry        false
&quot;United States&quot;

huangapple
  • 本文由 发表于 2023年6月5日 03:02:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定