第六章面向对象 第二篇

2018-11-25  本文已影响0人  黄敬理

-java继承思想的实现

package java_object.week2;

员工类

public class Employee {
private String name;
private int age;
private double salary = 2000;

public Employee(String name, int age, double salary) {
    this.name = name;
    this.age = age;
    this.salary = salary;
}
public Employee(){}
public double getSalary(){
    return salary;
}

}

经理类

public class Msnsger extends Employee {
private double bonus;
public void setBonus(double bonus){
this.bonus = bonus;
}
}

测试类

public class Test {
public static void main(String[] args) {
Msnsger msnsger = new Msnsger();
double sal = msnsger.getSalary();
System.out.println("继承的薪水为:"+sal);
}
}

-因为员工有的属性经理也有,所以使用继承可以避免重复代码的出现

上一篇下一篇

猜你喜欢

热点阅读