不太贊同 @xelz 的解釋。
實然不推出應然,從屬性與成員變量所通常具有的性質不推出屬性和成員變量應該具有這樣的性質。
換句話說,Java 的例子不能代表普遍意義上的、物件導向程式設計意義上的屬性和成員變量的區別。
要弄清這兩個概念的本質區別,就必須搞清楚它們分別是怎麼來的。
In abstract, property is that which belongs to or with something, whether as an attribute or as a component of said thing.
英文維基百科上說屬性(Property)是某物的品質/標誌或組成部分,這是屬性一詞的本意。
而成員變量(Member variables)則純粹是物件導向的程式設計中的術語,
In object-oriented programming, a member variable (sometimes called a member field) is a variable that is associated with a specific object, and accessible for all its methods (member functions). In class-based languages, these are distinguished into two types: if there is only one copy of the variable shared with all instances of the class, it is called a class variable or static member variable; while if each instance of the class has its own copy of the variable, the variable is called an instance variable.[1]
從物件導向程式設計層面上,成員變量只需是關聯到特定對象的變量,可供成員函數訪問,根本不需要訪問控制作前提,也更不需要成爲私有的。
成員變量可以看作物件導向程式設計的基本要素,是從代碼層面上說的;
而屬性則來自形而上學,是從語義上說的。
具體到不同的特定的物件導向的實現,也有細節上的區別,但萬萬不可以偏概全。
成員變量和屬性是兩個層面的概念,不理解二者的區別,不是缺少開發經驗,而是形而上學基礎太差。