Home  >  Article  >  Backend Development  >  Introduction to public properties of classes

Introduction to public properties of classes

巴扎黑
巴扎黑Original
2017-07-17 15:58:061536browse

1. Concept

We mentioned earlier the private attributes of the class, which are those that cannot be directly accessed in the class. But what if properties that can be directly accessed are public properties? Not really. The properties in the __init__() constructor are basically accessible to the outside world, but they are not public properties. So what are public attributes?

Definition: Refers to properties that can be accessed by all objects belonging to this class, called public properties.

2. Attributes

2.1 Member attributes

class Person(object):

    def __init__(self, name, job, phone, address):
        self.name = name    #  成员属性,属于某个实例对象
        self.job = job
        self.phone = phone
        self.__address = address

    def get_private(self):
        return self.__address

    def sayhi(self):
        print("hell,%s" % self.name)

p1 = Person('Bigberg', 'Doctor', '8833421', 'hz')
p2 = Person('Ashlex', 'Police', '8833232', 'bj')
print(p1.job, p2.job)

# 输出
Doctor Police

We defined them under the Person class Two objects, p1 and p2. Obviously we have no way for p1 to access the job attribute of p2, that is, Police. Then attributes such as self.name in the constructor __init__() are called member attributes.

2.2 Public properties

class Person(object):
    nationality = 'CN'   # 定义公有属性

    def __init__(self, name, job, phone, address):
        self.name = name
        self.job = job
        self.phone = phone
        self.address = address

    def sayhi(self):
        print("hell,%s" % self.name)

p1 = Person('Bigberg', 'Doctor', '8833421', 'hz')
p2 = Person('Ashlex', 'Police', '8833232', 'bj')

print(p1.nationality)
print(p2.nationality)

# 输出

CN
CN

For public properties, the values ​​obtained by all instance objects accessing them are the same. .

3. Characteristics of public attributes

We can not only access, but also change public attributes.  

class Person(object):
    nationality = 'CN'   # 定义公有属性

    def __init__(self, name, job, phone, address):
        self.name = name
        self.job = job
        self.phone = phone
        self.address = address

    def sayhi(self):
        print("hell,%s" % self.name)

p1 = Person('Bigberg', 'Doctor', '8833421', 'hz')
p2 = Person('Ashlex', 'Police', '8833232', 'bj')

print(Person.nationality)  # 调用 公有属性
Person.nationality = 'us'   # 改变 公有属性
print(Person.nationality)

#输出

CN 
us

 

3.1 Single instance call and change public properties

# 依据上例

print("--------Befoer change---------")
print(Person.nationality)
print(p1.nationality)
print(p2.nationality)

print("--------after change---------")
print(Person.nationality)
p1.nationality = 'JP'
print(p1.nationality)
print(p2.nationality)


# 输出

---- ----Befoer change---------
CN
CN
CN
--------after change---------
US
JP
US

We can understand p1 very well before modification, because everyone is calling the public attribute nationality of class Person, so the nationality attributes of p1 and p2 They are the same, both are 'CN'. But why did the nationality attribute of p2 not change after p1 modified the public attribute?

 

When we define a Person class, it actually already exists in the memory. , of course also contains the public properties of this class. When the initial instance p1 calls the nationality attribute of class Person, it directly refers to the memory address of nationality in the class instead of adding a new attribute called nationality.

As shown below:

print(id(Person.nationality))
print(id(p1.nationality))
print(id(p2.nationality))
print(Person.nationality, p1.nationality, p2.nationality)

#输出
1751236836128
1751236836128
1751236836128
CN CN CN

This can explain why p2 also changes when the nationality in the Person class is changed to 'US'. Because it directly references the value in memory.

 

p1.nationality = 'JP'

After p1 directly assigns the nationality attribute, instance p1 actually adds a new member variable to itself, called nationality. It's just that their names are the same, but there is no connection between the two, and even the memory addresses are different.

# p1.nationality = 'JP'
print(id(Person.nationality))
print(id(p1.nationality))
print(id(p2.nationality))
print(Person.nationality, p1.nationality, p2.nationality)

#输出
2434579585096
2434579585152
2434579585096
US JP US

Therefore, p1.nationality='JP' does not modify the public attributes of class Person, but creates a new member attribute for itself. Therefore, the change of p1 has an impact on the class. Public properties have no effect.

The above is the detailed content of Introduction to public properties of classes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn