英文:
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<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, 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 "Monkey{" +
"name='" + name + '\'' +
", gender='" + gender + '\'' +
", age='" + age + '\'' +
", weight='" + weight + '\'' +
", tailLength='" + tailLength + '\'' +
", height='" + height + '\'' +
", bodyLength='" + bodyLength + '\'' +
", species='" + species + '\'' +
", acquisitionDate='" + acquisitionDate + '\'' +
", acquisitionCountry='" + acquisitionCountry + '\'' +
", inServiceCountry='" + inServiceCountry + '\'' +
", trainingStatus='" + trainingStatus + '\'' +
", reserved=" + reserved +
'}';
}
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='Steve', gender='male', age='24', weight='15.4', tailLength='12', height='20', bodyLength='16', species='capuchin', acquisitionDate='04-26-1999', acquisitionCountry='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")
.acquisitionCountry("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, 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 "Monkey{" +
"name='" + name + '\'' +
", gender='" + gender + '\'' +
", age='" + age + '\'' +
", weight='" + weight + '\'' +
", tailLength='" + tailLength + '\'' +
", height='" + height + '\'' +
", bodyLength='" + bodyLength + '\'' +
", species='" + species + '\'' +
", acquisitionDate='" + acquisitionDate + '\'' +
", acquisitionCountry='" + acquisitionCountry + '\'' +
", inServiceCountry='" + inServiceCountry + '\'' +
", trainingStatus='" + trainingStatus + '\'' +
", reserved=" + reserved +
'}';
}
}
}
答案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 "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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论