一:静态属性
@property 封装逻辑,像“调用普通属性”一样
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 def cal_area(self): # print('%s住的%s总面积是%s' % (self.owner, self.name, self.width * self.length)) return self.width * self.length r1=Room('厕所','alex',100,100,100000) r2=Room('公共厕所','yuanhao',1,1,100000) print(r1.cal_area) #不用r1.cal_area() print(r2.cal_area)
@classmethod 与实例无关,只是类调用,不能调用属性
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_volume(self): return self.length * self.heigh * self.width def test(self): print('from test',self.name) @classmethod def tell_info(cls,x): print(cls) print('--》',cls.tag,x) r3=Room('厨房','alex1',122,111,13) print(r3.cal_volume) # Room.test(r3) Room.tell_info(10)
@staticmethod 类的工具包,不能访问类属性也不能访问实例属性
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_volume(self): return self.length * self.heigh * self.width @classmethod def tell_info(cls,x): print(cls) print('--》',cls.tag,x) @staticmethod def wash_body(a,b,c): print('%s %s %s正在洗澡'%(a,b,c)) def test(x,y): print(x,y) Room.wash_body('alex','yuanhao','wusir') print(Room.__dict__) r1=Room('厕所','alex',100,100,100000) Room.test(1,2) r1.test(1,2) #出错
二:组合 用组合的方式建立了类与组合的类之间的关系 作用是类与类之间的关联
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 class Teacher: def __init__(self,name,age,wage,course): self.name =name self.age =age self.wage =wage self.course=course s1=School('oldboy','北京') s2=School('oldboy','南京') s3=School('oldboy','东京') c1=Course('linux',10,'1h',s1) t1=Teacher('alex',18,18000,c1) print(c1.__dict__) print(c1.school.name) print(t1.__dict__) print('[%s]在[%s]里面教【%s】'%(t1.name,c1.school.name,t1.course.name)) msg=''' 1 老男孩 北京校区 2 老男孩 南京校区 2 老男孩 东京校区 ''' 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))
若用lower方式实现类之间的关联
class School: def __init__(self,name,addr): self.name=name self.addr=addr self.course_list=[] def zhao_sheng(self): print('%s正在招生'%self.name) class Course: def __init__(self,name,price,period): self.name=name self.price=price self.period=period s1=School('oldboy','北京') s2=School('oldboy','南京') s3=School('oldboy','东京') c1=Course('linux',10,'1h') c2=Course('python',10,'1h') s1.course_list.append(c1) s1.course_list.append(c2) print(s1.__dict__) for course_obj in s1.course_list: print(course_obj.name,course_obj.price)
三:面向对象编程是三大特点:
- 继承 :单继承 多继承
- 多态
- 封装
当类之间有显著不同时,用组合的方式
当类之间有许多相同功能,提取共同的功能组成基类,用继承的方式
继承含义一:继承+派生(减少重复代码,易出现强耦合)
继承含义二:接口继承
继承顺序例子:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class A(object): def test(self): print('from A') class B(A): def test(self): print('from B') class C(A): def test(self): print('from C') class D(B): def test(self): print('from D') class E(C): def test(self): print('from E') class F(D,E): # def test(self): # print('from F') pass f1=F() f1.test() print(F.__mro__) #只有新式才有这个属性可以查看线性列表,经典类没有这个属性 #新式类继承顺序:F->D->B->E->C->A #经典类继承顺序:F->D->B->A->E->C #python3中统一都是新式类 #pyhon2中才分新式类与经典类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import abc class All_file(metaclass=abc.ABCMeta): @abc.abstractclassmethod def read(self): pass def write(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') m1=Mem() m1.read() m1.write()
四:在子类中调用父类
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('开动了') print('开动了') print('开动了') class Subway(Vehicle): def __init__(self,name,speed,load,power,line): # Vehicle.__init__(self,name,speed,load,power) super().__init__(name,speed,load,power) # super(Subway,self).__init__(name,speed,load,power) self.line=line def show_info(self): print(self.name,self.line) def run(self): Vehicle.run(self) super().run() #不用加self,不用写父类名字 print('%s %s 线 开动了'%(self.name,self.line)) line13=Subway('北京地铁','10km/s',1000000000,'电',13) #实例化 line13.show_info() line13.run()