Re: [問題] Object的clone()問題
其實整篇的重點在於對 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
03/13 08:18, 1F
討論串 (同標題文章)
完整討論串 (本文為第 10 之 10 篇):
java 近期熱門文章
PTT數位生活區 即時熱門文章