一、组合
组合:把另外一个类的对象赋值给当前对象的属性
组合表达的是一种有的关系
ps: 继承:is-a
组合:has-a
多态:归一化,通过继承实现
class Teacher: def __init__(self, name, age, gender, level): self.name = name self.age = age self.gender = gender self.level = level def tell(self): print("%s:%s" % (self.name, self.age)) class Student: def __init__(self, name, age, gender): self.name = name self.age = age self.gender = gender class Course: def __init__(self, name, price, period): self.name = name self.price = price self.period = period def tell(self): print('<%s:%s:%s>' % (self.name, self.price, self.period)) tea1 = Teacher("egon", 18, "male", 10) stu1 = Student("xxx", 19, "male") python = Course("python开放", 30000, "3mons") linux = Course("linux课程", 30000, "3mons") tea1.courses = [python,linux] stu1.course = python # tea,stu # 超级对象 # stu1.course.tell() for course_obj in tea1.courses: course_obj.tell()
二、多态
同一种事物有多种形态
例如:动物这种事物有多种形态,如人\狗\猪
特性: 我们可以在不考虑某一个对象具体类型的前提下,直接使用该对象
1、父类有的功能,子类一定有
# import abc # # class Animal(metaclass=abc.ABCMeta): # @abc.abstractmethod # def speak(self): # pass # # @abc.abstractmethod # def run(self): # pass # # # Animal() # Animal的作用是用来制定标准的 # # class People(Animal): # def speak(self): # print("啊啊啊啊") # # def run(self): # print("咻咻咻...") # # class Dog(Animal): # def giao(self): # print("汪汪汪") # # class Pig(Animal): # def heheng(self): # print("哼哼哼") # peo1=People() # d1=Dog() # p1=Pig() # peo1.jiao() # d1.giao() # p1.heheng() # peo1.speak() # d1.speak() # p1.speak() # def speak(animal): # animal.speak() # # speak(peo1) # speak(d1) # # speak(p1)
2、鸭子类型:duck
# class People: # def speak(self): # print("啊啊啊啊") # # def run(self): # print("咻咻咻...") # # class Dog: # def speak(self): # print("汪汪汪") # # def run(self): # print("狂奔...") # # class Pig: # def speak(self): # print("哼哼") # # def run(self): # print("咣咣咣...") # # # peo1=People() # d1=Dog() # p1=Pig() # peo1.run() # d1.run() # p1.run()
class Cpu: def read(self): pass def write(self): pass class Process: def read(self): pass def write(self): pass class Disk: def read(self): pass def write(self): pass
三、一切皆对象
数据类型 == 类
# x = 11 # x=int(11) # print(int) # class Foo: # pass # print(Foo) x = [1,2,3] # list([1,2,3]) y = [111,222] # list([1,2,3]) # x.append(4) # y.append(3333) # list.append(x,4) # list.append(y,333) # print(x) # print(y) print(type(x))
四、面向对象高级
1、内置函数
is
isinstance判断类型
issubclass判断是否是子类
x = 111 print(type(x) is int) print(isinstance(x,int)) class Bar: pass class Foo(Bar): pass print(issubclass(Foo,Bar))
2、内置方法
内置方法都是在满足某种条件下自动触发的
(1)__str__
class People: def __init__(self, name, age): self.name = name self.age = age def __str__(self): # print('===>') return "<%s:%s>" %(self.name,self.age) obj = People("egon", 18) print(obj) # print(obj.__str__())
(2)__del__
class People: def __init__(self, name, age,f): self.name = name self.age = age self.f = f def __del__(self): #注意运行顺序 print('===>') # 回收资源 self.f.close() obj = People("egon", 18,open("a.txt",'w',encoding='utf-8')) del obj # print('运行完毕...')
五、反射
反射:获取程序里的对象,并且把它取出来
dir(对象)
hasattr
getattr
setattr
delattr
用法举例:
class BlackMedium: feature='Ugly' def __init__(self,name,addr): self.name=name self.addr=addr def sell_house(self): print('%s 黑中介卖房子啦,傻逼才买呢,但是谁能证明自己不傻逼' %self.name) def rent_house(self): print('%s 黑中介租房子啦,傻逼才租呢' %self.name) b1=BlackMedium('万成置地','回龙观天露园') #检测是否含有某属性 print(hasattr(b1,'name')) print(hasattr(b1,'sell_house')) #获取属性 n=getattr(b1,'name') print(n) func=getattr(b1,'rent_house') func() # getattr(b1,'aaaaaaaa') #报错 print(getattr(b1,'aaaaaaaa','不存在啊')) #设置属性 setattr(b1,'sb',True) setattr(b1,'show_name',lambda self:self.name+'sb') print(b1.__dict__) print(b1.show_name(b1)) #删除属性 delattr(b1,'addr') delattr(b1,'show_name') delattr(b1,'show_name111')#不存在,则报错 print(b1.__dict__)
class Foo: def __init__(self, x, y): self.x = x self.y = y def f1(self): print('from f1') obj = Foo(111, 222) #反射 # res = dir(obj) # print(res) # print(obj.__dict__[res[-1]]) # print(obj.__dict__['x']) # import re # # for attr in dir(obj): # if not re.search("^__.*__$",attr): # res=getattr(obj,attr) # print(res) # print(hasattr(obj,'x')) # print(getattr(obj,'xxx',None)) # obj.xxx # setattr(obj,"xxx",1111) # obj.xxx = 1111 # # delattr(obj,"xxx") # del obj.xxx # m=__import__("time") # m.sleep(3) # getattr(m,'sleep')(3)
---27---