day25-python之继承组合

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__)

 

posted @ 2019-05-26 16:03  有猿人  阅读(307)  评论(0编辑  收藏  举报