零基础入门python3.7 ——基础知识总结(十八) 类属性和实例属性

Violet ·
更新时间:2024-09-20
· 796 次阅读

无论是类属性还是类方法。 无法像普通的函数或者变量一样在类的外部直接使用。可以将类看作是一个独立的空间。那么类属性就是在类中定义的变量。类方法就是在类中定义的函数。

一。类属性的划分 在类中且在函数体外定义的变量称之为类属性或者类变量 在类中且在函数体内部以seif.变量名定义的变量称之为实例属性或者实例变量 在类中且在函数体内部以变量名=变量值的形式定义的变量称之为局部变量 二 类属性 class Person: name = '小张', age = 24 类属性的特点是所有实例化的对象都共享相同的变量 class Person: name = '小张', age = 24 a = Person() print(a.name) b = Person() print(b.name) ('小张',) ('小张',) 通过类名访问和修改类属性 class Person: name = '小张', age = 24 print(Person.name) print(Person.age) Person.name = 'Jack' Person.age = 25 print(Person.name) print(Person.age) ('小张',) 24 -------------------------------- Jack 25 通过对象访问类属性 class Person: name = '小张', age = 24 a = Person() print(a.name) b = Person() print(b.name) b.name = 'jack' print(b.name) print(a.name) ('小张',) ------------------------ ('小张',) ------------------------ jack ('小张',) 通过类名修改类属性的值会影响所有实例对象 class Person: name = '小张', age = 24 a = Person() b = Person() print(a.name) print(a.age) print(b.name) print(b.age) Person.name = 'jack' Person.age = 39 print(a.name) print(a.age) print(b.name) print(b.age) ('小张',) 24 ('小张',) 24 ---------------------- jack 39 jack 39 可以动态的为对象和类添加类属性 class Person: name = '小张', age = 24 a = Person() b = Person() Person.city = "成都" print(Person.city) print(a.city) print(b.city) 成都 成都 成都 三。实例变量 class Person: def __init__(self, name, age): self.name = name, self.age = age 实例变量的特点是只作用于调用方法的对象。 class Person: def __init__(self, name, age): self.name = name, self.age = age def addCity(self): self.city = "成都" a = Person("jack", 34) b = Person("jack", 34) a.addCity() print(a.city) print(b.city) 成都 Traceback (most recent call last): File "/Users/apple/Documents/重要文件/python3/python21.py", line 13, in print(b.city) AttributeError: 'Person' object has no attribute 'city' 实例变量只能通过对象访问不能通过类名访问 class Person: def __init__(self, name, age): self.name = name, self.age = age def addCity(self): self.city = "成都" a = Person("jack", 34) a.addCity() print(a.city) print(Person.city) 成都 Traceback (most recent call last): File "/Users/apple/Documents/重要文件/python3/python21.py", line 12, in print(Person.city) AttributeError: type object 'Person' has no attribute 'city' 通过类对象可以访问类但是无法修改类属性的值。原因在于通过类对象修改的值不是真正的修改类属性中的值。而是在新的实例中定义变量。 class Person: name = "小王", age = 34 a = Person() a.name = "Alice" print(a.name) print(Person.name) Alice ('小王',)

注意:示例变量和类变量可以同名。但是此时无法通过对象调用类属性。所以不推荐使用对象调用类属性

通过实例对象修改实例变量的值。不会影响其他实例对象 class Person: name = "小王", age = 34 a = Person() b = Person() a.city = "成都" print(a.city) print(b.city) 成都 Traceback (most recent call last): File "/Users/apple/Documents/重要文件/python3/python21.py", line 10, in print(b.city) AttributeError: 'Person' object has no attribute 'city' ------------------------------ class Person: name = "小王", age = 34 a = Person() b = Person() a.name = 'jack' print(a.name) print(b.name) jack ('小王',) 四。局部变量 class Person: def add(self, number): sun = 100 + number print("这是一个局部变量", sun) a = Person() a.add(100) 这是一个局部变量 200

局部变量只能在当前的类方法中使用。函数执行完之后局部变量就会被销毁。

雯倾浅忆 原创文章 38获赞 177访问量 4万+ 关注 私信 展开阅读全文
作者:雯倾浅忆



属性 零基础 类属性 Python3 Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号