java面向对象作业4

题目4:

分析以下需求,并用代码实现
1.项目经理类Manager
属性:
姓名name
工号id
工资salary
奖金bonus
行为:
工作work()
2.程序员类Coder
属性:
姓名name
工号id
工资salary
行为:
工作work()
要求:
1.按照以上要求定义Manager类和Coder类,属性要私有,生成空参、有参构造,setter和getter方法
2.定义测试类,在main方法中创建该类的对象并给属性赋值(演示两种方法:setter方法和构造方法)
3.调用成员方法,打印格式如下:
工号为123基本工资为15000奖金为6000的项目经理一龙正在努力的做着管理工作,分配任务,检查员工提交上来的代码…
工号为135基本工资为10000的程序员方便正在努力的写着代码…

参考代码:

public class Manager {
    private String name;
    private String id;
    private int salary;
    private int bonus;
    public Manager(){}
    public void work(){
        System.out.println("工号为"+id+"基本工资为"+salary+
                "奖金为"+bonus+"的项目经理"+name+"正在努力的做" +
                "着管理工作,分配任务,检查员工提交上来的代码。。。");
    }
    public Manager(String name, String id, int salary, int bonus) {
        this.name = name;
        this.id = id;
        this.salary = salary;
        this.bonus = bonus;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    public int getBonus() {
        return bonus;
    }
    public void setBonus(int bonus) {
        this.bonus = bonus;
    }
}

public class Coder {
    private String name;
    private String id;
    private int salary;
    public  void work() {
        System.out.println("工号为"+id+"基本工资为"+salary+"的程序员"+name+"正在努力的写着代码......");
    }
    public Coder(){}
    public Coder(String name, String id, int salary) {
        this.name = name;
        this.id = id;
        this.salary = salary;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
}

public class TestMandC {
    public static void main(String[] args) {
        Manager ma = new Manager();
        Coder co = new Coder();
        ma.setName("一龙");
        ma.setId("123");
        ma.setSalary(15000);
        ma.setBonus(6000);
        co.setName("程序员");
        co.setId("135");
        co.setSalary(10000);
        System.out.println(ma.getName());
        System.out.println(ma.getId());
        System.out.println(ma.getSalary());
        System.out.println(ma.getBonus());
        System.out.println(co.getName());
        System.out.println(co.getId());
        System.out.println(co.getSalary());
        ma.work();
        co.work();
    }
}
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页