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);分别是什么意思?
this.name=new String(name);这句话等于this.name=name;就是将name赋值给成员变量name,super(name)意思就是在子类构造方法中调用父类构造方法,如果你不写,可能会出错,一般情况下会默认super(),没有参数的情况下
不理解
this.name = new String(name)
重新開啟一個記憶體空間存放name 的資訊
super(name)
父類的name拿來使用
我想因該是這樣吧,但因該不用new String(name)吧