问答详情
源自:10-3 Java 中的抽象类

求解,大神

public abstract class Employee{

    private String name;

    public Employee (String name){

    this.name=new String(name);

}

    public String getName(){

        return name;

}

    public abstract double enrning();

}

public final calss Boss extends Employee{

    private double weeeklySalary;

public Boss(String name,double weeklySalary){

    super(name);

    setWeeklySalary(WeeklySalary);

}

public void setWeeklySalary(double WeeklySalary){

if(WeeklySalary<0) this.WeeklySalary=0;

else this.WeeklySalary=WeeklySalary;

}

public double earnings(){

    return WeeklySalary;

}

public String toString(){

    return"Boss"+getName()+"每周挣"+WeeklySalary+"元";

}

}

父类中的 this.name=new String(name);

子类中的 super(name);分别是什么意思?


提问者:pengzeming 2016-04-27 14:13

个回答

  • 冰镇西瓜
    2016-05-02 15:23:13

    this.name=new String(name);这句话等于this.name=name;就是将name赋值给成员变量name,super(name)意思就是在子类构造方法中调用父类构造方法,如果你不写,可能会出错,一般情况下会默认super(),没有参数的情况下

  • pengzeming
    2016-04-27 23:29:01

    不理解

  • loryyou
    2016-04-27 14:51:14

    this.name = new String(name) 

    重新開啟一個記憶體空間存放name 的資訊

    super(name)

    父類的name拿來使用

    我想因該是這樣吧,但因該不用new String(name)吧