属性和成员变量的概念
本来觉得属性和成员变量还是能分清的
直到看了《深入理解Yii2.0》
的第一章节的第一段就凌乱了
http://www.digpage.com/property.html
已附图
求有经验的发开解释下。。
它到底要表达什么。。
回复内容:
本来觉得属性和成员变量还是能分清的
直到看了《深入理解Yii2.0》
的第一章节的第一段就凌乱了
http://www.digpage.com/property.html
已附图
求有经验的发开解释下。。
它到底要表达什么。。
按书上的意思
类 People
有一个 成员变量 age
人
有一个 属性 年龄
一个是类结构(代码)层面的,一个是语义(逻辑)层面的
=============
我不太赞同上述解释,个人的理解,属性是被暴露
(非公开)的成员变量,而成员变量(大部分情况下)应该始终是私有的
Java的例子
<code>class People { private Date birthday; // 生日,成员变量 public int getAge() { // 年龄,只读属性 return daysBetween(birthday, today); } private String name; // 姓名,成员变量 public String getName() { // 姓名,读写属性 return name; } public void setName(String name) { check(name); // 写校验 this.name = name; } } </code>
属性的读写需要在类中相应的getter/setter进行校验、加工处理等, 以及控制属性是否只读、只写还是可读写
不太贊同 @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]
從物件導向程式設計層面上,成員變量只需是關聯到特定對象的變量,可供成員函數訪問,根本不需要訪問控制作前提,也更不需要成爲私有的。
成員變量可以看作物件導向程式設計的基本要素,是從代碼層面上說的;
而屬性則來自形而上學,是從語義上說的。
具體到不同的特定的物件導向的實現,也有細節上的區別,但萬萬不可以偏概全。
成員變量和屬性是兩個層面的概念,不理解二者的區別,不是缺少開發經驗,而是形而上學基礎太差。
看了截图。。我开始觉得自己就没学好中文。。
- 题主 本来能够理解 属性和成员变量。相信并不是不能理解他想表达的意思。
-
JAVA
C#
之类的编程语言都有这种属性 = 成员变量 + 方法
-
题主 问的应该是
比如,我们说People类有一个成员变量 int $age ,表示年龄。那么这里年龄就是属性 , $age 就是成员变量。
有没有起到完美解答属性
和成员变量
的关系。
我的回答是当然没有,从比如里 我推不出属性
和成员变量
的关系。但如果看了整个文章,后者理解了属性
和成员变量
的关系来推这个比如。其实也没这个必要了。 对新手来说,的确很有迷惑性。不过好歹人家后面写了那么多,多少还是能看懂的。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
