1.类属性
class Student(object):
# 类属性 被所有的类和类对象共同使用
score = 100
def __init__(self):
##实例属性实例化对象所拥有的
self.age =18
if __name__ == '__main__':
# 实列化一个学生对象
lh=Student()
# 实列化对象调用类属性
print(lh.score)
tyf=Student()
# 实列对象化调用类属性
print(tyf.score)
# 类可以自己使用类属性
print(Student.score)
# 类不能使用实列属性
#print(Student.age)
2.类属性的修改
class Dog(object):
# 类属性
tooth = 10
if __name__ == '__main__':
dh =Dog()
# 使用实列化对象 修改类属性
dh.tooth = 12
# 看一看 tooth 有没有被改变
print(Dog.tooth)
# 使用类 修改类属性
Dog.tooth = 14
print(Dog.tooth)

4393

被折叠的 条评论
为什么被折叠?



