类属性
类属性是从属于 类对象 的属性,也叫 类变量 ,类属性从属于类对象,可以被所有实例对象共享、
类属性的定义方式:
class 类名:
类变量名 = 初始值
在类中或者类的外面,通过:类名.类变量名 来读写
class Student():
com = 'XSE' # 类属性
max = 0 # 类属性
count = 0
def __init__(self,name,score):
Student.count += 1
self.name = name # 实例属性
self.score = score
Student.max = Student.max + 1
def a_score(self): # 实例方法
print(Student.com)
print(self.name,self.score)
s1 = Student('小明', 88) # s1 是实例对象,自动调用__init__方法
s1.a_score()
s2 = Student('小红', 80)
s3 = Student('小强', 81)
print(Student.count)
栈 | 堆 |
s1:3432 |
ID:3432 name: score: a_core() Student 类的类型对象(type) com: max: a_score 方法: 代码信息 |