class Employee{
private String name;
private int salary;
public Employee(String name,int salary){
this.name=name;
this.salary=salary;
}
public String getDtetails(){
return"Name:"+name+"\nSalary:"+salary;
}
}
//定义Employee的子类Manager
class Manager extends Employee{
private String department;
public Manager(String name,int salary,String department){
super(name,salary);//调用父类Employee的构造方法
this.department=department;
}
public String getDetails(){
//调用父类的成员方法
return super.getDetails()+"\nDepartment:"+department;
}
}
public class TestSuper{
public static void main(String[]args){
Manager m=new Manager("Tom",2000,"Finance");
System.out.println(m.getDetails());
}
}
我把你的代码复制到eclipse中运行了一下,发现只是少写了一个字母,改好就能运行了
//调用父类的成员方法
return super.getDetails()+"\nDepartment:"+department;
就上面部分getDetails()方法少了一个字母t,应该是getDtetails()
结果: