对象下—练习2
作者:互联网
Employee.java
package com.atguigu.exer1;
/*
* 编写一个Employee类,声明为抽象类,
包含如下三个属性:name,id,salary。
提供必要的构造器和抽象方法:work()。
*
*
*/
public abstract class Employee {
private String name;
private int id;
private double salary;
public Employee() {
super();
}
public Employee(String name, int id, double salary) {
super();
this.name = name;
this.id = id;
this.salary = salary;
}
public abstract void work();
}
EmployeeTest.java
package com.atguigu.exer1;
/*
* 请使用继承的思想,设计CommonEmployee类和Manager类,要求类中提供必要的方法进行属性访问。
*/
public class EmployeeTest {
public static void main(String[] args) {
//多态
Employee manager = new Manager("库克", 1001, 5000, 50000);
manager.work();
CommonEmployee commonEmployee = new CommonEmployee();
commonEmployee.work();
}
}
Menager.java
package com.atguigu.exer1;
/*
* 对于Manager类来说,他既是员工,还具有奖金(bonus)的属性。
*/
public class Manager extends Employee{
private double bonus;//奖金
public Manager(double bonus) {
super();
this.bonus = bonus;
}
public Manager(String name, int id, double salary, double bonus) {
super(name, id, salary);
this.bonus = bonus;
}
@Override
public void work() {
System.out.println("管理员工,提供公司运行的效率");
}
}
CommonEmployee.java
package com.atguigu.exer1;
public class CommonEmployee extends Employee {
@Override
public void work() {
System.out.println("员工在一线车间生产产品");
}
}
标签:salary,name,对象,练习,id,bonus,Employee,public 来源: https://www.cnblogs.com/fenxiangyuan/p/14397146.html