Java编写职工和经理工资的练习——继承和多态
职工类(Employee)
要求:包含name、age和salary(月薪),并且完成构造方法、相应的getter、setter方法以及获得收入方法(getIncome)。 注:职工的收入就是工资。
经理类(Manager)
要求:包含name、age和salary(月薪)、bonus(奖金),完成构造方法、相应的getter、setter方法以及获得收入方法(getIncome)。 注:经理的收入=工资+奖金。
构造Employee类和Manager类方法、属性
Employee类
class Employee{ protected String name; protected int age; protected double salary;//月薪 public Employee(String name,int age,double salary) { this.setName(name); this.setAge(age); this.setSalary(salary); } public Employee(){ this(null,0,0); } public static void main(String[] args) { // TODO 自动生成的方法存根 } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public double getIncome() { // TODO 自动生成的方法存根 return salary; } }
Manager类
class Manager extends Employee{ private double bonus; public void setBonus(double bonus) { this.bonus=bonus; } Manager(String name,int age,double salary,double bonus){ super(name,age,salary); this.bonus=bonus; } Manager(){ super(); bonus=0; } public double getIncome() { return super.getIncome()+bonus; } } class CountPay{ private static double pay; public static void doCount(Employee e) { setPay(getPay() + e.getIncome()); } public static void doCount(Manager m) { setPay(getPay() + m.getIncome()); } public static double getPay() { return pay; } public static void setPay(double pay) { CountPay.pay = pay; } }
主函数——各种方式调用的输出形式
main主函数
public static void main(String[] args) { System.out.println("======================================================"); System.out.println("=====第1种形式====="); Employee zhang=new Employee("张三",20,200); System.out.println(zhang.getSalary());//200.0 System.out.println(zhang.getIncome());//200.0 Manager wang=new Manager("李四",30,3000,1500); System.out.println(wang.getSalary());//3000.0 System.out.println(wang.getIncome());//4500.0 System.out.println("======================================================"); System.out.println("=====第2种形式====="); Employee e1=zhang; System.out.println(e1.getSalary()); System.out.println(e1.getIncome()); Manager e2=wang; System.out.println(e2.getSalary()); System.out.println(e2.getIncome()); System.out.println("======================================================"); System.out.println("=====第3种形式====="); Employee[] employees=new Employee[] { new Employee("张三",20,200), new Manager("李四",30,3000,1500) }; for(int i=0;i<employees.length;i++) { System.out.println(employees[i]); } System.out.println("======================================================"); System.out.println("=====第4种形式====="); System.out.println(new Employee("张三",20,200)); System.out.println(new Manager("李四",30,3000,1500)); System.out.println("======================================================"); System.out.println("=====第5种形式====="); CountPay.doCount(new Employee("张三",20,200)); CountPay.doCount(new Manager("李四",30,3000,1500)); }
Result
上一篇:
IDEA上Java项目控制台中文乱码