2013-04-16 67 views
0

因此,要解釋我的代碼 - >抽象類的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; 
} 
+2

難道你不能讓'抽象'員工類有抽象的getter方法嗎?這樣,擴展Employee類的類將不得不定義它們自己的getter行爲。 –

+0

將對象轉換爲適當的類型。 ((AdminWorker)emp).getFixedBonus() – dnault

回答

3

我覺得的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)

+1

這就是我所需要的!謝謝:) –

2

你應該getFixedBonus()getPercentBonus()方法添加到你的員工,每個返回0,那麼其他工人類可以覆蓋他們。

其實,一個更好的方法是使這是一個interface,讓所有的類(包括Employee)的實施interface。然後他們可以執行extend a base類,該類還實現了接口但提供了默認值。

public static interface Staff { 
    public double getFixedBonus(); 
    public double getPercentageBonus(); 
    public List<Staff> getMinions(); 
} 

// Base class - no bonus by default. 
public static class Worker implements Staff { 

    @Override 
    public double getFixedBonus() { 
    return 0; 
    } 

    @Override 
    public double getPercentageBonus() { 
    return 0; 
    } 

    @Override 
    public List<Staff> getMinions() { 
    return Collections.EMPTY_LIST; 
    } 

} 

public static class Administrator extends Worker implements Staff { 
    double fixedBonus = 2.13; 

    @Override 
    public double getFixedBonus() { 
    return fixedBonus; 
    } 

} 

public static class Salesperson extends Worker implements Staff { 
    double percentageBonus = 3.14; 

    @Override 
    public double getPercentageBonus() { 
    return percentageBonus; 
    } 

} 

public static class Manager extends Worker implements Staff { 
    List<Staff> minions; 

    @Override 
    public List<Staff> getMinions() { 
    return minions; 
    } 
} 

不要被陰暗面使用instanceof和鑄造。你會後悔你的決定。 oop的全部意義在於,即使你不知道你在做什麼樣的對象,但你仍然可以在它們上面工作。

+0

這是非常公平的,特別是對於像getPercentageBonus()或getFixedBonus()這樣的適用於員工有意義的東西。我會爭辯說給每個員工一個getMinions()方法有點不必要,但我想它與您爲管理員和銷售員所做的相符。格倫,從長遠來看,這可能是更好的方式。 – mdierker

0

,使選擇你的客戶端代碼很多更加簡單(和清潔 - 的instanceof和鑄造是醜陋的)是聲明一個默認的實現爲每個特定的方法Employee,例如EmployeefixedBonus()返回零,但壓倒一切它在Sales