Java子类与继承问题,构造器进不去
 package cn.campsg.java.experiment.entity;

public class Employe {
    private String name;
    private int level;
    private int salary;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getLevel() {
        return level;
    }
    public void setLevel(int level) {
        this.level = level;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    public Employe() {
        super();
    }
    public Employe(String name, int level, int salary) {
        super();
        this.name = name;
        this.level = level;
        this.salary = salary;
    }
    public void work(){
        System.out.println(this.name+"做好了本职工作!");
    }
}

package cn.campsg.java.experiment.entity;

public class Hrstaff extends Employe {
    public Hrstaff() {
    }
        public Hrstaff(String name){
        new Employe(name,5,5000);
    }
    public void paySalary(Employe em){
        int level = em.getLevel();
        if(level>=1&&level<=5){
            em.setSalary(em.getSalary()+500);
        }
        if(level>=6&&level<=9){
            em.setSalary(em.getSalary()+800);
        }
        if(level>=10&&level<=15){
            em.setSalary(em.getSalary()+1000);
        }
        else if(level>15||level<=0){
            System.out.println("不存在对应的职级,无法发放额外浮动薪水");
        }
    }
}
package cn.campsg.java.experiment;
import cn.campsg.java.experiment.entity.Employe;
import cn.campsg.java.experiment.entity.Hrstaff;
public class MainClass {
    public static void main(String[] args) {
        Employe em1 = new Employe("南帝",11,12000);
        Employe em2 = new Employe("北丐",1,4500);
        Employe em3 = new Employe("中神",10,6300);
        Hrstaff HR = new Hrstaff("HR")
        em1.work();
        em2.work();
        em3.work();
        HR.work();
        System.out.println("HR开始发放工资:");
        HR.paySalary(em1);
        System.out.println("员工"+em1.getName()+"的薪水:"+em1.getSalary());
        HR.paySalary(em2);
        System.out.println("员工"+em2.getName()+"的薪水:"+em2.getSalary());
        HR.paySalary(em3);
        System.out.println("员工"+em3.getName()+"的薪水:"+em3.getSalary());
        HR.paySalary(HR);
        System.out.println("员工"+HR.getName()+"的薪水:"+HR.getSalary());
    }

}

代码中 Hrstaff HR = new Hrstaff("HR");感觉“HR”这个参数没有传进去,怎么改都进不去构造器。求教

2个回答

public Hrstaff(String name){
new Employe(name,5,5000);
}
super()函数

public Hrstaff(String name){
    new Employe(name,5,5000);  // 这里你只是创建了一个对象
}

你应该使用 super 关键字使用父类的构造方法

public Hrstaff(String name){
    super(name,5,5000);  // 调用父类的构造方法
}

这样你的数据就可以保存下来了

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!

相似问题