因此,要解釋我的代碼 - >抽象類的getter和setter在Java
我有一個抽象類Employee類型的,它有自己的領域,如姓名。
然後,我有從員工延伸的類Admin,Sales和Manager。這些每個都有自己的字段,例如管理員的fixedBonus,銷售額的百分比和管理員的員工數組列表。
然後我有一個驅動程序類,其中包含員工的ArrayList,因此它包含管理員,銷售和管理員。
我的問題出現在Arraylist元素中。假設我從ArrayList中獲得一個Admin類的員工,並嘗試使用getterFixedBonus()獲取他們的fixedBonus字段,我簡直不能。我怎麼能做到這一點,所以我可以使用類特定的getter和setter,而不僅僅是繼承的?這裏是我的代碼的副本,其中出現問題。具體來說,問題出在switch語句的情況5和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;
}
難道你不能讓'抽象'員工類有抽象的getter方法嗎?這樣,擴展Employee類的類將不得不定義它們自己的getter行爲。 –
將對象轉換爲適當的類型。 ((AdminWorker)emp).getFixedBonus() – dnault