类的继承:员工和老板

 1 package 面向对象;
 2 //创建父类
 3 public class Employee {
 4 
 5     //定义成员变量
 6     private String name;
 7     private int age;
 8     private double salary;
 9     //获取和写入
10     
11     public String getName() {
12         return name;
13     }
14 
15     public void setName(String name) {
16         this.name = name;
17     }
18 
19     public int getAge() {
20         return age;
21     }
22 
23     public void setAge(int age) {
24         this.age = age;
25     }
26 
27     public double getSalary() {
28         return salary;
29     }
30 
31     public void setSalary(double salary) {
32         this.salary = salary;
33     }
34 
35     //构造方法
36     public Employee()
37     {
38         
39     }
40     //方法
41     public boolean buzhu()
42     {
43         boolean b = false;
44         if(age>=50)
45         {
46             System.out.println("符合补助的年龄条件");
47             b = true;
48         }
49         else
50         {
51             System.out.println("不符合补助的年龄条件");
52         }
53         return b;
54     }
55     
56 }

 

 

 1 package 面向对象;
 2 //创建子类
 3 public class Manager extends Employee {
 4 
 5     //定义成员变量
 6     private double bonus;
 7     //获取、写入
 8     public double getBonus()
 9     {
10         return bonus;
11     }
12     public void setBonus(double bonus)
13     {
14         this.bonus = bonus;
15     }
16     //方法
17     public void nianxin(double salary)//参数是调用父类的成员变量
18     {
19         double nianxin = bonus + salary;
20         System.out.println("老板的工资是:"+salary+"\n老板的月薪是:"+nianxin);
21     }
22 }

测试

 1 package 面向对象;
 2 
 3 public class Test {
 4 
 5     public static void main(String[] args) {
 6         //实例化fu类,创建对象
 7         Employee employee = new Employee();
 8         
 9         employee.setName("哆来咪");
10         System.out.println("输出员工1的名字:"+employee.getName());
11         employee.setAge(49);
12         System.out.println("输出员工1的年龄:"+employee.getAge());
13         employee.buzhu();
14         
15         employee.setName("哆来咪发扫拉西");
16         System.out.println("输出员工2的名字:"+employee.getName());
17         employee.setAge(59);
18         System.out.println("输出员工2的年龄:"+employee.getAge());
19         employee.buzhu();
20         System.out.println();
21         //实例化子类
22         Manager manager = new Manager();
23         manager.setName("老板");
24         manager.setAge(45);
25         manager.setSalary(5000);
26         manager.setBonus(3000);
27         System.out.println("输出老板的名字:"+manager.getName());
28         System.out.println("输出老板的年龄:"+manager.getAge());
29         System.out.println("输出老板的工资:"+manager.getSalary());
30         System.out.println("输出老板的的奖金:"+manager.getBonus());
31         manager.nianxin(manager.getSalary());
32 
33     }
34 
35 }

 

 

posted on 2016-01-20 23:55  年少不上班  阅读(291)  评论(0编辑  收藏  举报

导航