2019.9.21 java小知识
今天在做educoder
又给自己补了个漏洞
1 class Employee { 2 private String name; 3 private String birth; 4 private String position; 5 public Employee(String name,String birth, String position){ 6 this.name = name; 7 this.birth = birth; 8 this.position = position; 9 } 10 public String getName(){ 11 return name; 12 } 13 public String getBirth(){ 14 return birth; 15 } 16 public String getPosition(){ 17 return position; 18 } 19 } 20 21 class Salary extends Employee { 22 private double salary; 23 public Salary(String name, String birth, String position, double salary){ 24 super(name, birth, position); 25 this.salary = salary; 26 } 27 public void introduction(){ 28 System.out.print("员工姓名:" + this.name() + " 出生年月:" + this.birth + " 职位:" + this.position + " 薪水:" + this.salary); 29 } 30 }
在28行要调用父类的属性,会发现出现了错误
要调用父类的private属性必须要通过父类的方法
所以应该改为this.getName()。。。。