VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • python基础教程之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