java之继承练习 (基础)

发布时间:2024年01月20日

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("我的工作是炒菜");
    }

}

文章来源:https://blog.csdn.net/qq_62319385/article/details/135701310
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。