#一个Student类可以创建N多个Student类的实例对象,每个实例对象的属性值不同
class Student:
#Student 类对象
def __init__(self,name,age):#
self.name=name
self.age=age
def eat(self):
print(self.name+'在吃饭')
#Student 实例对象
stu1=Student('法外狂徒',30)
stu2=Student('李四',30)
print(id(stu1))
print(id(stu2))
print('-----为stu2动态添加性别属性---------')
stu2.gender='女'
print(stu1.name,stu1.age)
print(stu2.name,stu2.age,stu2.gender)
stu2.eat()
stu1.eat()
#定义在类之外的,称为函数
def show():
print('name')
stu1.show=show()#动态绑定方法
stu1.show
#动态绑定属性和方法
最新推荐文章于 2024-04-30 21:03:59 发布