gpt4 book ai didi

java - java 中的抽象类 getter 和 setter

转载 作者:行者123 更新时间:2023-12-01 17:24:32 24 4
gpt4 key购买 nike

所以,解释一下我的代码 ->

我有一个 Employee 类型的抽象类,它有自己的字段,例如姓名。

然后,我有从 Employee 扩展的 Admin、Sales 和 Manager 类。每个都有自己的字段,例如管理员的固定奖金、销售的百分比奖金和经理的员工数组列表。

然后我有一个驱动程序类,其中包含员工的 ArrayList,因此它包含管理员、销售人员和经理。

我的问题出现在 Arraylist 元素上。假设我从 ArrayList 中获取 Admin 类的一名员工,并尝试使用 getter getFixedBonus() 获取他们的fixedBonus 字段,我根本做不到。我怎样才能做到这一点,以便我可以使用特定于类的 getter 和 setter,而不仅仅是继承的 getter 和 setter?这是我的代码的副本,其中出现了问题。具体来说,问题出在 switch 语句的 case 5 和 case 6 中。

private Employee editAnEmployee(Employee emp) {
boolean exit = false;
boolean validChoice = false;
int choice = 0;
Boolean sure = false; // used in the switch statement
String newName;
double newValue;
Employee admin = new AdminWorker("0", "0", 0, 0, 0); // these are used
Employee sales = new SalesWorker("0", "0", 0, 0, 0, 0); // to check the
Employee manager = new Manager("0", "0", 0, 0); // class of the
// employee
// being sent
// in, to edit
// the fields
// the employee
// may hold
do {
do {
validChoice = true;
emp.toString();
StdOut.println("Which field would you like to edit?");
StdOut.println("1) First Name.");
StdOut.println("2) last Name.");
StdOut.println("3) Hourly rate.");
StdOut.println("4) Hours worked in the last week.");
if (emp.getClass().equals(admin)) {
StdOut.println("5) Fixed Bonus.");
} else if (emp.getClass().equals(sales)) {
StdOut.println("5) Percentage bonus on sales.");
StdOut.println("6) Value of sales made in the last week.");
} else if (emp.getClass().equals(manager)) {
StdOut.println("5) Department.");
}
StdOut.println("0) Exit.");
choice = StdIn.readInt();
if (choice < 0 || choice > 4) {
if (emp.getClass().equals(admin) && choice <= 5) {
validChoice = true;
} else if (emp.getClass().equals(sales) && choice <= 6) {
validChoice = true;
} else if (emp.getClass().equals(manager)) {
validChoice = true;
} else {
StdOut.println("You entered an invalid number! Try Again...");
validChoice = false;
}
}
} while (!validChoice);
switch (choice) {
case 0:
StdOut.println("You are now exiting...");
exit = true;
break;
case 1:
StdOut.println("The employees first name is: " + emp.getFirstName() + ". What would you like the first name to be now?");
newName = StdIn.readString();
StdOut.println("Are you sure you want to change " + emp.getFirstName() + " to " + newName +"?(y/n)");
sure = ynChoice();
if(sure) emp.setFirstName(newName);
break;
case 2:
StdOut.println("The employees last name is: " + emp.getLastName() + ". What would you like the last name to be now?");
newName = StdIn.readString();
StdOut.println("Are you sure you want to change " + emp.getLastName() + " to " + newName +"?(y/n)");
sure = ynChoice();
if(sure) emp.setLastName(newName);
break;
case 3:
StdOut.println("The employees hourly rate is: " + emp.getHourlyRate() + ". What would you like the hourly rate to be now?");
newValue = StdIn.readDouble();
StdOut.println("Are you sure you want to change " + emp.getHourlyRate() + " to " + newValue +"?(y/n)");
sure = ynChoice();
if(sure) emp.setHourlyRate(newValue);
break;
case 4:
StdOut.println("The employee has worked: " + emp.getHoursWorked() + " hours in the last week. What would you like that to be now?");
newValue = StdIn.readDouble();
StdOut.println("Are you sure you want to change " + emp.getHoursWorked() + " to " + newValue +"?(y/n)");
sure = ynChoice();
if(sure) emp.setHoursWorked(newValue);
break;
case 5:
if (emp.getClass().equals(admin)) {
StdOut.println("The employees fixed bonus is: " + emp.getFixedBonus() + ". What would you like that to be now?");
newValue = StdIn.readDouble();
StdOut.println("Are you sure you want to change " + emp.getFixedBonus() + " to " + newValue +"?(y/n)");
sure = ynChoice();
if(sure) emp.setHoursWorked(newValue);
} else if (emp.getClass().equals(sales)) {
StdOut.println("The employees percentage bonus is: " + emp.getPercentageBonus() + ". What would you like that to be now?");
newValue = StdIn.readDouble();
StdOut.println("Are you sure you want to change " + emp.getPercentageBonus() + " to " + newValue +"?(y/n)");
sure = ynChoice();
if(sure) emp.setHoursWorked(newValue);
} else if (emp.getClass().equals(manager)) {
StdOut.println("The employees fixed bonus is: " + emp.getFixedBonus() + ". What would you like that to be now?");
newValue = StdIn.readDouble();
StdOut.println("Are you sure you want to change " + emp.getFixedBonus() + " to " + newValue +"?(y/n)");
sure = ynChoice();
if(sure) emp.setHoursWorked(newValue);
}
break;
case 6:
StdOut.println("The employees sales in the last week are: " + emp.getLastWeeksSales() + ". What would you like that to be now?");
newValue = StdIn.readDouble();
StdOut.println("Are you sure you want to change " + emp.getLastWeeksSales() + " to " + newValue +"?(y/n)");
sure = ynChoice();
if(sure) emp.setHoursWorked(newValue);
break;
}
} while (!exit);
return emp;
}

最佳答案

我认为 instanceof 关键字可能对您的情况有用。

ArrayList<Employee> list; // This is initialized and has Employees

for (Employee person: list) {
if (person instanceof Admin) {
int fixedBonus = ((Admin) person).getFixedBonus();

// Do something here
}
}

这能回答你的问题吗? (你不需要 .getClass().equals(),这就是 instanceof 的作用:D )

关于java - java 中的抽象类 getter 和 setter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16048308/

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