class luffystudent: def __init__(self,name,sex,age): self.name=name self.sex=sex self.age=age school=‘luffycity‘ def learn(self,x): print(‘%s is learning %s‘ %(self.name,x)) def eat(self): print(‘is eating‘) def sleep(self): print(‘is sleeping‘) #后产生对象stu1=luffystudent(‘sunny‘,‘nan‘,‘28‘)stu2=luffystudent(‘alex‘,‘男‘,‘30‘)stu3=luffystudent(‘egon‘,‘nan‘,‘31‘)# print(stu1.__dict__)# print(stu2.__dict__)# print(stu3) #对象:技能与特征的结合体#类:是一系列对象相似的特征与技能结合体#类中的数据属性:是所有对象共有的# print(luffystudent.school,id(luffystudent.school))# print(stu1.school,id(stu1.school))# print(stu2.school,id(stu2.school))#类中的函数属性:是绑定到对象的使用的,绑定到不同对象,方法不同,对象调用绑定方法时#会把本身当做第一个对象传入,传给self# print(luffystudent.learn)#luffystudent.learn(stu1) # print(stu1.learn)stu1.learn(1)# print(stu2.learn)stu2.learn(2)# print(stu3.learn)
原文地址:https://www.cnblogs.com/sunny666/p/9633674.html
时间: 2024-10-31 12:52:27