作者adrianshum (Alien)
看板java
标题Re: [问题] Object的clone()问题
时间Tue Mar 11 21:38:27 2014
其实整篇的重点在於对 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
1F:推 michael47:请问如果用super可以让Child使用Parent的protected吗? 03/13 08:18