当前位置: 首页 > 图灵资讯 > 技术篇> 继承案例

继承案例

来源:图灵教育
时间:2023-05-31 09:12:59

案列需求:

继承案例_子类

///测试类publiccicc class Test2 {    public static void main(String[] args) {        coder c=new coder(张三,25,15000);        c.work();        Manager m=new Manager(李四,24,18000,5000);        m.work();    }}

////父亲publicccicc class Employee {    private String name;    private int age;    private double wages;    public Employee() {    }    public Employee(String name, int age, double wages) {        this.name = name;        this.age = age;        this.wages = wages;    }    public void work() {        System.out.println(“员工工作”;    }    /**     * 获取     *     * @return name     */    public String getName() {        return name;    }    /**     * 设置     *     * @param name     */    public void setName(String name) {        this.name = name;    }    /**     * 获取     *     * @return age     */    public int getAge() {        return age;    }    /**     * 设置     *     * @param age     */    public void setAge(int age) {        this.age = age;    }    /**     * 获取     *     * @return wages     */    public double getWages() {        return wages;    }    /**     * 设置     *     * @param wages     */    public void setWages(double wages) {        this.wages = wages;    }}

///程序员子类publicic class coder extends Employee {    public coder() {    }    public coder(String name, int age, double wages) {        super(name, age, wages);    }    public void work(){        System.out.println(”姓名为“”+super.getName()+”,年龄为“                +super.getAge()+",工资为"+super.getWages()+                程序员正在编写代码”;    }}

///项目经理子类publicic class Manager extends Employee{    double bonus;    public Manager() {    }    public Manager(String name, int age, double wages,double bonus) {        super(name, age, wages);        this.bonus=bonus;    }    public void work(){        System.out.println(”姓名为“”+super.getName()+”,年龄为“                +super.getAge()+",工资为"+super.getWages()+                ",奖金为"+bonus+“项目经理正在分配任务”;    }    /**     * 获取     * @return bonus     */    public double getBonus() {        return bonus;    }    /**     * 设置     * @param bonus     */    public void setBonus(double bonus) {        this.bonus = bonus;    }}