day25-python 之继承组合
1. 上节回顾
- class School:
- x=1
- def __init__(self,name,addr,type):
- self.Name=name
- self.Addr=addr
- self.Type=type
- def tell_info(self):
- print('学校的详细信息是: name:%s addr:%s' %(self.Name,self.Addr))
- # s1=School('oldboy','沙河','私立')
- # print(s1.__dict__)
- #
- # print(School.__dict__)
- #
- # s1.tell_info()
- # School.tell_info(s1)
2. 静态属性
- 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
- @property
- def cal1_area(self):
- return self.width * self.length
- def test(self):
- print('from test',self.name)
- @classmethod
- def tell_info(cls,x):
- print(cls)
- print('--》',cls.tag,x)#print('--》',Room.tag)
- # def tell_info(self):
- # print('---->',self.tag)
- @classmethod
- def tell_info1(cls,x):
- print(cls)
- print('-->>',cls.tag,x)
- # print(Room.tag)
- # Room.test(1) #1.name
- # r1=Room('厕所','alex',100,100,100000)
- # Room.tell_info(10)
- r1=Room('厕所','alex',100,100,100000)
- r2=Room('公共厕所','yuanhao',1,1,1)
- # print('%s 住的 %s 总面积是 %s' %(r1.owner,r1.name,r1.width*r1.length))
- # print('%s 住的 %s 总面积是 %s' %(r2.owner,r2.name,r2.width*r2.length))
- # r1.cal_area()
- # r2.cal_area()
- print(r1.cal_area)
- print(r2.cal_area)
- print(r1.name)
- print(r2.name)
3. 类方法
- 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
- @property
- def cal_area1(self):
- return self.width * self.length
- def test(self):
- print('from test',self.name)
- @classmethod
- def tell_info(cls,x):
- print(cls)
- print('--》',cls.tag,x)#print('--》',Room.tag)
- # def tell_info(self):
- # print('---->',self.tag)
- @classmethod
- def tell_info1(cls,x):
- print(cls)
- print('-->>',cls.tag,x)
- # print(Room.tag)
- # Room.test(1) #1.name
- r1=Room('厕所','alex',100,100,100000)
- # Room.test(r1)
- Room.tell_info(10)
4. 静态方法
- 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))
- @staticmethod
- def wash_body1(a,b,c):
- print('%s %s %s 正在洗澡'%(a,b,c))
- def test(x,y):
- print(x,y)
- # Room.wash_body('alex','yuanhao','wupeiqi')
- # print(Room.__dict__)
- r1=Room('厕所','alex',100,100,100000)
- #
- # print(r1.__dict__)
- # r1.wash_body('alex','yuanhao','wupeiqi')
- # Room.test(1,2)
- r1.test(1)
5. 组合
- # class Hand:
- # pass
- #
- # class Foot:
- # pass
- #
- # class Trunk:
- # pass
- #
- # class Head:
- # pass
- # class Person:
- # def __init__(self,id_num,name):
- # self.id_num=id_num
- # self.name=name
- # self.hand=Hand()
- # self.foot=Foot()
- # self.trunk=Trunk()
- # self.head=Head()
- # p1=Person('111111','alex')
- #
- #
- # print(p1.__dict__)
- # class School:
- # def __init__(self,name,addr):
- # self.name=name
- # self.addr=addr
- #
- # def zhao_sheng(self):
- # print('%s 正在招生' %self.name)
- #
- # class Course:
- # def __init__(self,name,price,period,school):
- # self.name=name
- # self.price=price
- # self.period=period
- # self.school=school
- # #
- # #
- # #
- # s1=School('oldboy','北京')
- # s2=School('oldboy','南京')
- # s3=School('oldboy','东京')
- #
- # # c1=Course('linux',10,'1h','oldboy 北京')
- # c1=Course('linux',10,'1h',s1)
- # #
- # print(c1.__dict__)
- # print(c1.school.name)
- # print(s1)
- class School:
- def __init__(self,name,addr):
- self.name=name
- self.addr=addr
- def zhao_sheng(self):
- print('%s 正在招生' %self.name)
- class Course:
- def __init__(self,name,price,period,school):
- self.name=name
- self.price=price
- self.period=period
- self.school = school
- s1=School('oldboy','北京')
- s2=School('oldboy','南京')
- s3=School('oldboy','东京')
- #
- # c1=Course('linux',10,'1h','oldboy 北京')
- c1=Course('linux',10,'1h',s1)
- msg='''
- 1 老男孩 北京校区
- 2 老男孩 南京校区
- 3 老男孩 东京校区
- '''
- while True:
- print(msg)
- menu={
- '1':s1,
- '2':s2,
- '3':s3
- }
- choice=input('选择学校 >>:')
- school_obj=menu[choice]
- name=input('课程名 >>:')
- price=input('课程费用 >>:')
- period=input('课程周期 >>:')
- new_course=Course(name,price,period,school_obj)
- print('课程 [%s] 属于 [%s] 学校' %(new_course.name,new_course.school.name))
6. 继承
- class Dad:
- '这个是爸爸类'
- money=10
- def __init__(self,name):
- print('爸爸')
- self.name=name
- def hit_son(self):
- print('%s 正在打儿子' %self.name)
- class Son(Dad):
- money = 1000000000009
- def __init__(self,name,age):
- self.name=name
- self.age=age
- def hit_son(self):
- print('来自儿子类')
- # print(Son.money)
- # Son.hit_son(Son('andy',12))
- # print(Dad.__dict__)
- # print(Son.__dict__)
- s1=Son('alex',18)
- # s1.hit_son()
- print(s1.money)
- print(Dad.money)
- print(s1.name)
- print(s1.money)
- print(s1.__dict__)
- s1.hit_son()
7. 接口继承
- import abc
- class All_file(metaclass=abc.ABCMeta):
- @abc.abstractmethod
- def read(self):
- pass
- @abc.abstractclassmethod
- def read1(self):
- pass
- @abc.abstractmethod
- def write(self):
- pass
- @abc.abstractclassmethod
- def write1(self):
- pass
- class Disk(All_file):
- def read(self):
- print('disk read')
- def write(self):
- print('disk write')
- class Cdrom(All_file):
- def read(self):
- print('cdrom read')
- def write(self):
- print('cdrom write')
- class Mem(All_file):
- def read(self):
- print('mem read')
- def write(self):
- print('mem write')
- class Mem1(All_file):
- def read(self):
- print('mem read')
- def write(self):
- print('mem write')
- #
- m1=Mem1()
- m1.read()
- m1.write()
8. 继承顺序
- #coding:utf-8
- class A:
- def test(self):
- print('A')
- pass
- class B(A):
- # def test(self):
- # print('B')
- pass
- class C(A):
- def test(self):
- print('C')
- pass
- class D(B):
- # def test(self):
- # print('D')
- pass
- class E(C):
- # def test(self):
- # print('E')
- pass
- class F(D,E):
- # def test(self):
- # print('F')
- pass
- f1=F()
- f1.test() #经典类: F->D->B->A-->E-->
- print(F.__mro__)
- #F-->D->B-->E--->C--->A 新式类
9. 在子类中调用父类的方法
- class Vehicle:
- Country='China'
- def __init__(self,name,speed,load,power):
- self.name=name
- self.speed=speed
- self.load=load
- self.power=power
- def run(self):
- print('开动啦')
- print('开动啦')
- class Subway(Vehicle):
- def __init__(self,name,speed,load,power,line):
- Vehicle.__init__(self,name,speed,load,power)
- self.line=line
- def show_info(self):
- print(self.name,self.speed,self.load,self.power,self.line)
- def run(self):
- Vehicle.run(self)
- print('%s %s 线, 开动啦' %(self.name,self.line))
- line13=Subway('北京地铁','10km/s',1000000000,'电',13)
- line13.show_info()
- line13.run()
10.super 方法的使用
- class Vehicle1:
- Country='China'
- def __init__(self,name,speed,load,power):
- self.name=name
- self.speed=speed
- self.load=load
- self.power=power
- def run(self):
- print('开动啦')
- print('开动啦')
- class Subway(Vehicle1):
- def __init__(self,name,speed,load,power,line):
- # Vehicle.__init__(self,name,speed,load,power)
- # super().__init__(name,speed,load,power) #super(__class__,self).__init__(name,speed,load,power)
- super(Subway,self).__init__(name,speed,load,power)
- self.line=line
- def show_info(self):
- print(self.name,self.speed,self.load,self.power,self.line)
- def run(self):
- # Vehicle.run(self)
- super().run()
- print('%s %s 线, 开动啦' %(self.name,self.line))
- line13=Subway('北京地铁','10km/s',1000000000,'电',13)
- line13.show_info()
- line13.run()
- print(line13.__class__)
来源: https://www.2cto.com/kf/201905/809951.html