- /*2:请用抽象类体现如下代码:
- 需求:公司中程序员有姓名,工号,薪水,工作内容。
- 项目经理除了有姓名,工号,薪水,还有奖金,工作内容。
- 思路:
- 员工(程序员、项目经理)
- 姓名 工号 薪水
- 工作
- 程序员 简单的继承
- 项目经理 简单继承+奖金
- */
- //员工抽象类
- abstract class Employee
- {
- private String name;
- private int num;
- private int salary;
- public Employee(){}
- public void setName(String name)
- {
- this.name=name;
- }
- public String getName()
- {
- return name;
- }
- public void setNum(int num)
- {
- this.num=num;
- }
- public int getNum()
- {
- return num;
- }
- public void setSalary(int salary)
- {
- this.salary=salary;
- }
- public int getSalary()
- {
- return salary;
- }
- public abstract void work();
- }
- interface Bonus
- {
- public abstract void bonus();
- }
- class Programmer extends Employee
- {
- public void work()
- {
- System.out.println("工作是撸代码");
- }
- }
- class ProjectManager extends Employee implements Bonus
- {
- public void work()
- {
- System.out.println("工作是指挥撸代码");
-
- }
- public void bonus()
- {
- System.out.println("发奖金");
- }
- }
- class EmployeeTest
- {
- public static void main(String[] args)
- {
- Employee e=new Programmer();
- e.setName("李 ");
- e.setNum(0777520);//?????
- e.setSalary(1000000);
- System.out.println("工号为"+e.getNum()+"程序员"+e.getName()+"的工资为"+e.getSalary());
- e.work();
- }
- }
复制代码 |
|