Re: [問題] Object的clone()問題

看板java作者 (Alien)時間11年前 (2014/03/11 21:38), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串10/10 (看更多)
其實整篇的重點在於對 protected 的認知。 剛好我最近在 stackoverflow 有答過類似的問題: http://stackoverflow.com/a/22140728/395202 簡單來說,protected 是 default (即 package) accessibility 加上 不同 package 的 subclass 可見。 重點是所謂不同package 的 subclass 的意思。 Java 裡,subclass 只可以看到該 subclass 裡的 protected member, 而不能看到其 parent class 的 protected member。 你會說,那 protected member 在 parent declare,那不就一樣了嗎? 不是 節錄自 Core Java However, the Manager class methods can peek inside the hireDay field of Manager objects only, not of other Employee objects. This restriction is made so that you can’t abuse the protected mechanism by forming subclasses just to gain access to the protected fields 即是,假設 Parent 有 protected member foo, Child extends Parent. Child 只能看到 Child 的 foo, 不能看到 Parent 的 foo (SO裡我的答案有比較詳細的例子) (當然前題是 Parent 與 Child 不同 package. 同 package 的話 package level accessibility 就已經把 protected 開出來任人觀賞了) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 223.19.45.84

03/13 08:18, , 1F
請問如果用super可以讓Child使用Parent的protected嗎?
03/13 08:18, 1F
文章代碼(AID): #1J7n7LGl (java)
文章代碼(AID): #1J7n7LGl (java)