java之继承练习 (基础)

1,java之继承练习 (基础)_第1张图片

public class test {
    public static void main(String[] args) {
        manager m = new manager(1,"jim",15000,1200);
        cooker c = new cooker(2,"marry",15000);
        m.work();
        c.work();
    }
}


public class employee {
    private int id;
    private String name;
    private int salary;

    public employee() {
    }

    public employee(int id, String name, int salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }
    public void work(){
        System.out.println("我的工作是");
    }
    public void eat() {
        System.out.println("我吃米饭");
    }
}

public class manager extends employee{
    private int bonus;

    public manager(int bonus) {
        this.bonus = bonus;
    }

    public manager(int id, String name, int salary, int bonus) {
        super(id, name, salary);
        this.bonus = bonus;
    }

    public int getBonus() {
        return bonus;
    }

    public void setBonus(int bonus) {
        this.bonus = bonus;
    }
    @Override//方法重写
    public void work(){
        System.out.println("我的工作是管理别人");
    }

}

public class cooker extends employee{
    public cooker() {
    }

    public cooker(int id, String name, int salary) {
        super(id, name, salary);
    }
    @Override//方法重写
    public void work(){
        System.out.println("我的工作是炒菜");
    }

}

你可能感兴趣的:(java语法,java)