day25-python之继承组合
程序员文章站
2022-04-03 08:46:04
1.上节回顾 2.静态属性 3.类方法 4.静态方法 5.组合 6.继承 7.接口继承 8.继承顺序 9.在子类中调用父类的方法 10.super方法的使用 ......
1.上节回顾
1 class school: 2 x=1 3 def __init__(self,name,addr,type): 4 self.name=name 5 self.addr=addr 6 self.type=type 7 8 def tell_info(self): 9 print('学校的详细信息是:name:%s addr:%s' %(self.name,self.addr)) 10 11 12 # s1=school('oldboy','沙河','私立') 13 14 # print(s1.__dict__) 15 # 16 # print(school.__dict__) 17 # 18 # s1.tell_info() 19 # school.tell_info(s1)
2.静态属性
1 class room: 2 tag=1 3 def __init__(self,name,owner,width,length,heigh): 4 self.name=name 5 self.owner=owner 6 self.width=width 7 self.length=length 8 self.heigh=heigh 9 10 @property 11 def cal_area(self): 12 # print('%s 住的 %s 总面积是%s' % (self.owner,self.name, self.width * self.length)) 13 return self.width * self.length 14 @property 15 def cal1_area(self): 16 return self.width * self.length 17 18 19 def test(self): 20 print('from test',self.name) 21 22 @classmethod 23 def tell_info(cls,x): 24 print(cls) 25 print('--》',cls.tag,x)#print('--》',room.tag) 26 # def tell_info(self): 27 # print('---->',self.tag) 28 @classmethod 29 def tell_info1(cls,x): 30 print(cls) 31 print('-->>',cls.tag,x) 32 33 # print(room.tag) 34 35 # room.test(1) #1.name 36 # r1=room('厕所','alex',100,100,100000) 37 # room.tell_info(10) 38 39 r1=room('厕所','alex',100,100,100000) 40 r2=room('公共厕所','yuanhao',1,1,1) 41 # print('%s 住的 %s 总面积是%s' %(r1.owner,r1.name,r1.width*r1.length)) 42 # print('%s 住的 %s 总面积是%s' %(r2.owner,r2.name,r2.width*r2.length)) 43 # r1.cal_area() 44 # r2.cal_area() 45 print(r1.cal_area) 46 print(r2.cal_area) 47 print(r1.name) 48 print(r2.name)
3.类方法
1 class room: 2 tag=1 3 def __init__(self,name,owner,width,length,heigh): 4 self.name=name 5 self.owner=owner 6 self.width=width 7 self.length=length 8 self.heigh=heigh 9 10 @property 11 def cal_area(self): 12 # print('%s 住的 %s 总面积是%s' % (self.owner,self.name, self.width * self.length)) 13 return self.width * self.length 14 @property 15 def cal_area1(self): 16 return self.width * self.length 17 18 19 def test(self): 20 print('from test',self.name) 21 22 @classmethod 23 def tell_info(cls,x): 24 print(cls) 25 print('--》',cls.tag,x)#print('--》',room.tag) 26 # def tell_info(self): 27 # print('---->',self.tag) 28 29 @classmethod 30 def tell_info1(cls,x): 31 print(cls) 32 print('-->>',cls.tag,x) 33 34 # print(room.tag) 35 36 # room.test(1) #1.name 37 r1=room('厕所','alex',100,100,100000) 38 # room.test(r1) 39 room.tell_info(10)
4.静态方法
1 class room: 2 tag=1 3 def __init__(self,name,owner,width,length,heigh): 4 self.name=name 5 self.owner=owner 6 self.width=width 7 self.length=length 8 self.heigh=heigh 9 10 @property 11 def cal_area(self): 12 # print('%s 住的 %s 总面积是%s' % (self.owner,self.name, self.width * self.length)) 13 return self.width * self.length 14 15 @classmethod 16 def tell_info(cls,x): 17 print(cls) 18 print('--》',cls.tag,x)#print('--》',room.tag) 19 # def tell_info(self): 20 # print('---->',self.tag) 21 22 @staticmethod 23 def wash_body(a,b,c): 24 print('%s %s %s正在洗澡' %(a,b,c)) 25 26 @staticmethod 27 def wash_body1(a,b,c): 28 print('%s %s %s正在洗澡'%(a,b,c)) 29 30 def test(x,y): 31 print(x,y) 32 33 # room.wash_body('alex','yuanhao','wupeiqi') 34 35 # print(room.__dict__) 36 37 38 r1=room('厕所','alex',100,100,100000) 39 # 40 # print(r1.__dict__) 41 # r1.wash_body('alex','yuanhao','wupeiqi') 42 43 # room.test(1,2) 44 r1.test(1)
5.组合
1 # class hand: 2 # pass 3 # 4 # class foot: 5 # pass 6 # 7 # class trunk: 8 # pass 9 # 10 # class head: 11 # pass 12 13 14 # class person: 15 # def __init__(self,id_num,name): 16 # self.id_num=id_num 17 # self.name=name 18 # self.hand=hand() 19 # self.foot=foot() 20 # self.trunk=trunk() 21 # self.head=head() 22 # p1=person('111111','alex') 23 # 24 # 25 # print(p1.__dict__) 26 27 # class school: 28 # def __init__(self,name,addr): 29 # self.name=name 30 # self.addr=addr 31 # 32 # def zhao_sheng(self): 33 # print('%s 正在招生' %self.name) 34 # 35 # class course: 36 # def __init__(self,name,price,period,school): 37 # self.name=name 38 # self.price=price 39 # self.period=period 40 # self.school=school 41 # # 42 # # 43 # # 44 # s1=school('oldboy','北京') 45 # s2=school('oldboy','南京') 46 # s3=school('oldboy','东京') 47 # 48 # # c1=course('linux',10,'1h','oldboy 北京') 49 # c1=course('linux',10,'1h',s1) 50 # # 51 # print(c1.__dict__) 52 # print(c1.school.name) 53 # print(s1) 54 55 56 57 58 59 60 61 62 63 64 65 66 67 class school: 68 def __init__(self,name,addr): 69 self.name=name 70 self.addr=addr 71 72 73 def zhao_sheng(self): 74 print('%s 正在招生' %self.name) 75 76 class course: 77 def __init__(self,name,price,period,school): 78 self.name=name 79 self.price=price 80 self.period=period 81 self.school = school 82 83 84 85 s1=school('oldboy','北京') 86 s2=school('oldboy','南京') 87 s3=school('oldboy','东京') 88 # 89 # c1=course('linux',10,'1h','oldboy 北京') 90 c1=course('linux',10,'1h',s1) 91 92 msg=''' 93 1 老男孩 北京校区 94 2 老男孩 南京校区 95 3 老男孩 东京校区 96 ''' 97 while true: 98 print(msg) 99 menu={ 100 '1':s1, 101 '2':s2, 102 '3':s3 103 } 104 choice=input('选择学校>>: ') 105 school_obj=menu[choice] 106 name=input('课程名>>: ') 107 price=input('课程费用>>: ') 108 period=input('课程周期>>: ') 109 new_course=course(name,price,period,school_obj) 110 print('课程【%s】属于【%s】学校' %(new_course.name,new_course.school.name))
6.继承
1 class dad: 2 '这个是爸爸类' 3 money=10 4 def __init__(self,name): 5 print('爸爸') 6 self.name=name 7 def hit_son(self): 8 print('%s 正在打儿子' %self.name) 9 10 class son(dad): 11 money = 1000000000009 12 def __init__(self,name,age): 13 self.name=name 14 self.age=age 15 16 def hit_son(self): 17 print('来自儿子类') 18 # print(son.money) 19 # son.hit_son(son('andy',12)) 20 # print(dad.__dict__) 21 # print(son.__dict__) 22 s1=son('alex',18) 23 # s1.hit_son() 24 print(s1.money) 25 print(dad.money) 26 print(s1.name) 27 print(s1.money) 28 print(s1.__dict__) 29 s1.hit_son()
7.接口继承
1 import abc 2 class all_file(metaclass=abc.abcmeta): 3 @abc.abstractmethod 4 def read(self): 5 pass 6 7 @abc.abstractclassmethod 8 def read1(self): 9 pass 10 11 @abc.abstractmethod 12 def write(self): 13 pass 14 15 @abc.abstractclassmethod 16 def write1(self): 17 pass 18 19 class disk(all_file): 20 def read(self): 21 print('disk read') 22 23 def write(self): 24 print('disk write') 25 26 class cdrom(all_file): 27 def read(self): 28 print('cdrom read') 29 30 def write(self): 31 print('cdrom write') 32 33 34 class mem(all_file): 35 def read(self): 36 print('mem read') 37 38 def write(self): 39 print('mem write') 40 41 class mem1(all_file): 42 def read(self): 43 print('mem read') 44 45 def write(self): 46 print('mem write') 47 # 48 m1=mem1() 49 m1.read() 50 m1.write()
8.继承顺序
1 #coding:utf-8 2 class a: 3 def test(self): 4 print('a') 5 pass 6 class b(a): 7 # def test(self): 8 # print('b') 9 10 pass 11 class c(a): 12 def test(self): 13 print('c') 14 pass 15 16 class d(b): 17 # def test(self): 18 # print('d') 19 pass 20 21 class e(c): 22 # def test(self): 23 # print('e') 24 pass 25 26 class f(d,e): 27 # def test(self): 28 # print('f') 29 pass 30 f1=f() 31 f1.test() #经典类:f->d->b->a-->e--> 32 33 print(f.__mro__) 34 35 #f-->d->b-->e--->c--->a新式类
9.在子类中调用父类的方法
1 class vehicle: 2 country='china' 3 def __init__(self,name,speed,load,power): 4 self.name=name 5 self.speed=speed 6 self.load=load 7 self.power=power 8 def run(self): 9 print('开动啦') 10 print('开动啦') 11 class subway(vehicle): 12 def __init__(self,name,speed,load,power,line): 13 vehicle.__init__(self,name,speed,load,power) 14 self.line=line 15 16 def show_info(self): 17 print(self.name,self.speed,self.load,self.power,self.line) 18 19 def run(self): 20 vehicle.run(self) 21 print('%s %s 线,开动啦' %(self.name,self.line)) 22 line13=subway('北京地铁','10km/s',1000000000,'电',13) 23 24 line13.show_info() 25 26 line13.run()
10.super方法的使用
1 class vehicle1: 2 country='china' 3 def __init__(self,name,speed,load,power): 4 self.name=name 5 self.speed=speed 6 self.load=load 7 self.power=power 8 def run(self): 9 print('开动啦') 10 print('开动啦') 11 class subway(vehicle1): 12 def __init__(self,name,speed,load,power,line): 13 # vehicle.__init__(self,name,speed,load,power) 14 # super().__init__(name,speed,load,power) #super(__class__,self).__init__(name,speed,load,power) 15 super(subway,self).__init__(name,speed,load,power) 16 self.line=line 17 def show_info(self): 18 print(self.name,self.speed,self.load,self.power,self.line) 19 def run(self): 20 # vehicle.run(self) 21 super().run() 22 print('%s %s 线,开动啦' %(self.name,self.line)) 23 line13=subway('北京地铁','10km/s',1000000000,'电',13) 24 line13.show_info() 25 line13.run() 26 27 print(line13.__class__)
上一篇: 明代中期名臣:揭秘项忠的生平经历
下一篇: 设计模式-外观模式模式