有人可以帮助我修复下面所附的代码吗?我已经编写了一个Java方法,该方法按姓氏雇用一个人,我的意思是该方法中传递的值应该是对象引用的姓(我们为类Employee
的对象)希望在公司聘用)。
但是,程序抱怨说姓氏不能解析为类型。现在,getSurname()
是在Employee
中定义的私有实例变量的方法,该变量是一个抽象类(以及两个子类Worker
和Officer
的超类,但这与问题)。有人愿意帮我吗?
public class CompanyArrayList {
private ArrayList<Employee> arrayList;
public CompanyArrayList(int employeeNumber) {
ArrayList<Employee> arrayList = new ArrayList<Employee>(employeeNumber);
}
public String hire(Employee employee.getSurname()) { // the object of this class will be generated in the "main" method.
for (int i = 0; i < employeeNumber; i++) {
if (!arrayList.contains(employee.getSurname())) {
arrayList.add(employee);
return "Hired"; // "return" terminates the execution of the method.
}
} // WHY?
}
public String fire(Employee employee) {
for (Employee i : arrayList) {
if (employee.getSurname().equals(i.getSurname())) {
arrayList.remove(employee);
return "Fired"; // returns nothing and terminates the method execution.
}
}
return "The person of the" + employee.getSurname() + " surname doesn't work in the company";
}
}
public abstract class Employee {
private String surname;
private float contract; // contract = workperiod
public String getSurname() { // Here we're asking about a surname of an employee
return surname;
}
public float getContract() {
return contract;
}
public Employee(String surname, float contract) {
this.surname = surname;
this.contract = contract;
}
public abstract float pay();
public abstract String group();
}
答案 0 :(得分:0)
将String hire(Employee employee.getSurname())
更改为String hire(Employee employee)
。
编辑:
关于您要按姓氏Employee
hire()
(我已经向您解释了为什么这是个坏主意)的public String hire(Employee employee) {
for (int i = 0; i < arrayList.size(); i++) { // you had employeeNumber instead of arraylist.size() but hire() method does not know anything about that variable
Employee temp = arraylist.get(i);
if (temp.getSurname().equals(employee.getSurname)) {
arrayList.add(employee);
return "Hired"; // "return" terminates the execution of the method.
}
}
}
:
mvn archetype:generate "-DinteractiveMode=false" "-DarchetypeGroupId=org.openjdk.jcstress" "-DarchetypeArtifactId=jcstress-java-test-archetype" "-DarchetypeVersion=0.4" "-DgroupId=org.sample" "-DartifactId=test" "-Dversion=1.0"