class student(object): #定义student类,继承object父类
name=‘student in class one’ #直接定义student类的属性(类属性)
print(student.name) #打印类的name属性
student in class one
直接定义类属性
class student(object): #与前面相同功能的实现方法
def init(self): #通过self变量–实例变量,添加实例属性
self.name=‘student in class one’ #给self.name属性赋值
n=student() #实例化类
print(n.name) #属于student类的n变量的name属性
student in class one
通过实例变量给所有调用类的实例绑定实力属性
class student(object):
pass
类属性
student.name=‘class 1’ #给类绑定类属性,可被所有student类变量拥有
实例属性
n=student() #实例化student类
n.name=‘class 2’ #给n绑定name属性,只能用于n
一起执行
print(n.name)结果是class 2 #类属性被后来实例n的赋值的实例# # #属性覆盖