php类属性 类成员变量的区别,属性和成员变量的概念

不太贊同 @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]

從物件導向程式設計層面上,成員變量只需是關聯到特定對象的變量,可供成員函數訪問,根本不需要訪問控制作前提,也更不需要成爲私有的。

成員變量可以看作物件導向程式設計的基本要素,是從代碼層面上說的;

而屬性則來自形而上學,是從語義上說的。

具體到不同的特定的物件導向的實現,也有細節上的區別,但萬萬不可以偏概全。

成員變量和屬性是兩個層面的概念,不理解二者的區別,不是缺少開發經驗,而是形而上學基礎太差。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值