一、day24复习
class school: x=1 #__init__初始化函数,用来帮类实例化一个具体的对象 def __init__(self,name,addr): #前面的Name是一个需要封到字典里面的一个key,后面的name是传过来的一个值 self.Name=name self.Addr=addr def tell_info(self): print('学校的详细信息是:name:%s addr:%s'%(self.Name,self.Addr)) #实例化的过程s1=school('charon','qqhr')print(school.__dict__)print(s1.__dict__)s1.tell_info()结果:{'__module__': '__main__', 'x': 1, '__init__':, 'tell_info': , '__dict__': , '__weakref__': , '__doc__': None}{'Name': 'charon', 'Addr': 'qqhr'}学校的详细信息是:name:charon addr:qqhr
二、静态属性
封装操作好处可以将背后的操作隐藏起来
class Room: def __init__(self,name,owner,width,length,heigh): self.name=name self.owner=owner self.width=width self.length=length self.heigh=heigh @property #@property加上这个后面调用不用加括号 def cal_area(self): ######print('%s 住的 %s 总面积是%s' %(self.owner,self.name,self.width*self.length)) return self.width*self.heighr1=Room('WC','pluto',100,12,123)#####r1.cal_areaprint(r1.cal_area)print(r1.name)结果:12300WC
调用cal_area函数相当于调用了直接参数,将背后的操作隐藏起来,@property可以封装你函数的逻辑,像是调用普通逻辑一样s
三、类方法
类有属性,包含数据属性跟函数属性,
class Room: tag=1 def __init__(self,name,owner,width,length,heigh): self.name=name self.owner=owner self.width=width self.length=length self.heigh=heigh @property #@property加上这个后面调用不用加括号 def cal_area(self): ######print('%s 住的 %s 总面积是%s' %(self.owner,self.name,self.width*self.length)) return self.width*self.heigh def test(self,name): print('from test',self.name) @classmethod #cls接收一个类名,类方法,自动传类名参数 def tell_info(cls,x): print(cls) print('----->',cls.tag,x)print(Room.tag)Room.tell_info(10)结果:1-----> 1 10
四、静态方法
class Room: tag=1 def __init__(self,name,owner,width,length,heigh): self.name=name self.owner=owner self.width=width self.length=length self.heigh=heigh @property def cal_area(self): # print('%s 住的 %s 总面积是%s' % (self.owner,self.name, self.width * self.length)) return self.width * self.length @classmethod def tell_info(cls,x): print(cls) print('--》',cls.tag,x)#print('--》',Room.tag) # def tell_info(self): # print('---->',self.tag) @staticmethod #相当于类的工具包 def wash_body(a,b,c): print('%s %s %s正在洗澡' %(a,b,c)) r1=Room('厕所','alex',100,100,100000)r1.wash_body('charon','pluto','to')Room.wash_body('charon','pluto','to')结果:charon pluto to正在洗澡charon pluto to正在洗澡
@staticmethod静态方法只是名义上归属类管理,不能使用类变量和实例变量,是类的工具包
五、小节
静态属性:@property把函数封装成一个数据属性的形似,让外部调的时候看不到内部逻辑,因为可以传递self。所有静态属性既可以访问实例属性,又可以访问类属性
类方法:@classmethod函数默认参数写成cls,cls代表类。类能访问类的数据属性,类的函数属性,不能访问到实例的属性,实例是类里面(下的)包的东西,不能从外面作用域访问的里面作用域
静态方法:@staticmethod静态方法只是名义上归属类管理,不能使用类变量和实例变量,是类的工具包,不能访问类属性,也不能访问实例属性
六、组合
class School: def __init__(self,name,addr): self.name=name self.addr=addrclass Course: def __init__(self,name,price,preiod,school): self.name=name self.price=price self.preiod=preiod self.school=schools1=School('oldboy','BJ')s2=School('oldboy','NJ')s3=School('oldboy','DJ')#c1=Course('linux',10,'1h',s1)#print(c1.__dict__)##print(s1)#print(c1.school.name)msg='''1.oldboy BJschool2.oldboy NJschool3.oldboy DJschool'''while True: print(msg) menu={ '1':s1, '2':s2, '3':s3,} choice=input('>>>>>choine school:') school_obj=menu[choice] name = input('>>>>course name:') price = input('>>>>>>>course price:') preiod = input('>>>>>course preiod:') new_course=Course(name,price,preiod,school_obj) print('course [%s] of [%s] school' % (new_course.name1,new_course.school.name))