gpt4 book ai didi

java - 哪个类导致 "undefined for the type"错误?

转载 作者:行者123 更新时间:2023-12-02 08:48:22 25 4
gpt4 key购买 nike

Eclipse 告诉我最后一行的 getName() 的“Chore 类型未定义”。我不明白问题出在哪里。我还有一个 Chore 类,那么它是否试图从 Chore 类中提取 Name ?或者我的 People 类有问题吗? (还添加了 Chore 类)

        System.out.printf("%s's Chores: %n", person.getName());
for(int i=0; i<person.getNumChores(); i++) {
System.out.println(person.getChores()[i].getName()); //error here
}

人物类别:

public class People {
private Chore chores[] = new Chore[48];
private int numChores = 0;

private final String name;
private final int birthYear;
private int age;

public void addChore(Chore chore) {
if(numChores == 48) {
System.out.println("All chores have been assigned.");
}
else {
chores[numChores++] = chore;
}
}

public People(String name, int birthYear, int age) {
this.name = name;
this.birthYear = birthYear;
this.age = age;
}

public String getName() {
return name;
}

public int getBirthYear() {
return birthYear;
}

public int setAge(int birthYear) {
int age = 2020 - birthYear;
return age;
}

public int getAge(){
return age;
}

public int getNumChores() {
return numChores;
}

public Chore[] getChores() {
return chores;
}
}

这是杂务类:

public class Chore{

private final String choreName;
private final String difficulty;
private final String timesPerWeek;
private String location;
private String assigned;

public Chore(String location, String choreName, String timesPerWeek, String difficulty){
this.location = location;
this.choreName = choreName;
this.timesPerWeek = timesPerWeek;
this.difficulty = difficulty;
this.assigned = "N";
}

public Chore(String choreName, String timesPerWeek, String difficulty){
this(choreName, timesPerWeek, difficulty, "Whole house");
this.assigned = "N";
}

public String getChoreName(){
return choreName;
}
public String getDifficulty(){
return difficulty;
}

public String getTimesPerWeek(){
return timesPerWeek;
}
public String getLocation(){
return location;
}

public String setAssigned(){
String assigned = "Y";
return assigned;
}

public String getAssigned(){
return assigned;
}
}

最佳答案

您在 Chore 中没有 getName() 方法。

替换

System.out.println(person.getChores()[i].getName());

System.out.println(person.getChores()[i].getChoreName());

关于java - 哪个类导致 "undefined for the type"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60939671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com