繼承的getter and setter
class Father1 {
private int number = 0;
public int getNum() {
return number;
}
public void setNum(int number) {
this.number = number;
}
}
class Son1 extends Father1 {
private int number = 200;
}
public class TestPolymorphismEx11 {
public static void main(String[] args) {
Father1 a = new Son1();
System.out.println(a.getNum());
}
}
//run完結果為0
想請問如何繼承Father的getter setter方法來存取Son的屬性
我思考是private是同class下才能存取,也就是必定要做Son的getter跟setter。
但如果Son有大量時每個都設置不是DRY很嚴重嗎?
請問有什麼解法
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.171.213.96
※ 文章網址: https://www.ptt.cc/bbs/java/M.1457186691.A.081.html
→
03/05 22:26, , 1F
03/05 22:26, 1F
→
03/05 22:26, , 2F
03/05 22:26, 2F
→
03/05 22:31, , 3F
03/05 22:31, 3F
→
03/05 22:55, , 4F
03/05 22:55, 4F
→
03/05 23:30, , 5F
03/05 23:30, 5F
→
03/06 00:12, , 6F
03/06 00:12, 6F
推
03/06 10:07, , 7F
03/06 10:07, 7F
→
03/06 10:07, , 8F
03/06 10:07, 8F
→
03/06 10:09, , 9F
03/06 10:09, 9F
java 近期熱門文章
PTT數位生活區 即時熱門文章