我刚刚进入Java并且我很快就会有一个项目到期,并继续遇到同样的错误。我将创建一个学生,教师,员工目录,可以添加新条目,更新它们,并使用to String()方法打印它们。我也被建议使用treeMap集合。该错误涉及.getKey()方法,无法找到它。我将包含我的代码,并非常感谢任何输入,以帮助我解决这个问题。先感谢您。
import java.util.TreeMap;
import java.util.*;
import java.util.Map.Entry;
import java.util.Iterator;
import java.util.Map;
/**
* Write a description of class Persons here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Directory {
private String firstName, lastName;
private TreeMap<String, Persons> schoolDirectory;
private int numberInDirectory;
public Directory() {
schoolDirectory = new TreeMap<String, Persons>();
}
public String getKey() {
return lastName + firstName;
}
public void addPerson(Persons newPersons) {
schoolDirectory.put(Persons.getKey(), newPersons);
numberInDirectory++;
}
public void addPerson(Staff newStaff) {
schoolDirectory.put(newStaff.getKey(), newStaff);
numberInDirectory++;
}
public int getNumber() {
return numberInDirectory;
}
public class Persons {
// instance variables - replace the example below with your own
private String firstName;
private String lastName;
private String email;
}
}
/**
* Write a description of class People here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class People extends Directory {
private String firstName, lastName, emailAddress, phoneNumber, ID;
public People(String firstName, String lastName, String emailAddress, String phoneNumber, String ID) {
this.firstName = firstName;
this.lastName = lastName;
this.emailAddress = emailAddress;
this.phoneNumber = phoneNumber;
this.ID = ID;
}
public String getKey() {
return lastName + firstName;
}
}
/**
* Write a description of class Students here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Students extends People {
// instance variables - replace the example below with your own
private String classlevel;
private int SudentID;
}
/**
* Write a description of class Faculty here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Faculty extends People {
private String RoomNumber;
private String EmployeeStatus;
private String ProgramOfInstruction;
public Faculty(String firstName, String lastName, String emailAddress, String phoneNumber, String ID, String room, String status, String instruction) {
super(firstName, lastName, emailAddress, phoneNumber, ID);
RoomNumber = room;
EmployeeStatus = status;
ProgramOfInstruction = instruction;
}
}
/**
* Write a description of class Staff here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Staff extends People {
private String OfficeNumber;
private String JobTitle;
public Staff(String firstName, String lastName, String emailAddress, String phoneNumber, String ID, String office, String position) {
super(firstName, lastName, emailAddress, phoneNumber, ID);
OfficeNumber = office;
JobTitle = position;
}
}
答案 0 :(得分:0)
首先改变:
import java.util.TreeMap;
import java.util.*;
import java.util.Map.Entry;
import java.util.Iterator;
import java.util.Map;
只是:
import java.util.*
(这只是为了保持整洁)
然后我建议使用Map或HashMap而不是treeMap(但我不熟悉treeMap)
编辑:只需将Persons.getKey()
更改为newPersons.getKey()
你试图静态调用它。
希望这有帮助!