payroll.java

来自「Practical Java也是一本和J2ME手机游戏开发相关的书」· Java 代码 · 共 54 行

JAVA
54
字号
interface Employee
{
  public int salary();
  public int bonus();
}

class Manager implements Employee
{
  private static final int mgrSal = 40000;
  private static final int mgrBonus = 0;
  public int salary()
  {
    return mgrSal;
  }
  public int bonus()
  {
    return mgrBonus;
  }
}

class Programmer implements Employee
{
  private static final int prgSal = 50000;
  private static final int prgBonus = 10000;
  public int salary()
  {
    return prgSal;
  }

  public int bonus()
  {
    return prgBonus;
  }
}

class Payroll
{
  public int calcPayroll(Employee emp)
  {
    //Calculate the bonus. No instanceof check needed.
    return emp.salary() + emp.bonus();
  }

  public static void main(String args[])
  {
    Payroll pr = new Payroll();
    Programmer prg = new Programmer();
    Manager mgr = new Manager();
    System.out.println("Payroll for Programmer is " +
                       pr.calcPayroll(prg));
    System.out.println("Payroll for Manager is " +
                       pr.calcPayroll(mgr));
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?