Re: [問題]有關private屬性
※ 引述《Contactman.bbs@bbs.badcow.com.tw (小白退散!)》之銘言:
: 這個寫法是合法的, 無論是 C++ / C# / JAVA 都是合法的.
: 所謂的可見度, 是針對 class level 而言, 而非 object/instance level 而言.
: 對 class A 來講, 他所有的 private member data 都可以被另一個 class A 的
: Instance 存取.
: 也就是說, scope 的修飾詞只對不同的 class level 有意義.
: 原因很簡單, Complier 無法預測 runtime 的行為, 而 object / instance 是屬於
: runtime 時期的產物.
object/instance 是 runtime 的產物也不代表
compiler 不能對其作出檢查吧?.
另, 說 accessibility 是 class level, Java 中有一個例外.
Java 中的 protected keyword 是針對個別 instance 的.
child class 的 instance 只能存取本身 instance 的 parent
class protected member,
e.g.
class Parent {
protected int parentInt;
}
class Child {
void foo() {
this.parentInt = 10; //valid
}
void bar(Child anotherChild) {
anotherChild.parentInt = 5; //ERROR
}
}
Alien
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 202.22.246.26
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 5 之 8 篇):