面向对象-组合
1.什么是组合
组合指的是某一个对象拥有一个属性,该属性的值是另外一个类的对象
class Foo:
xxx = 111 class Bar:
yyy = 222 obj = Foo()
obj.attr = Bar() print(obj.xxx)
>>>111
print(obj.attr.yyy)
>>>222
2.为何要用组合
通过为某一个对象添加属性(属性的值是另外一个类的对象)的方式,可以间接地将两个类关联/整合/组合到一起
从而减少类与类之间代码冗余
class Foo1:
pass
class Foo2:
pass
class Foo3:
pass class Bar:
pass obj_from_bar=Bar() obj1=Foo1()
obj2=Foo2()
obj3=Foo3() obj1.attr1=obj_from_bar
obj2.attr2=obj_from_bar
obj3.attr3=obj_from_bar
3.如何用组合
class OldboyPeople:
school = 'Oldboy' def __init__(self, name, age, sex, ):
self.name = name
self.age = age
self.sex = sex class OldboyStudent(OldboyPeople):
def __init__(self, name, age, sex, score=0):
OldboyPeople.__init__(self, name, age, sex)
self.score = score
self.courses = [] def choose_course(self):
print('%s choosing course' % self.name) def tell_all_course(self):
print(('学生[%s]选修的课程如下' % self.name).center(50, '='))
for obj in self.courses:
obj.tell_info()
print('=' * 60) class OldboyTeacher(OldboyPeople):
def __init__(self, name, age, sex, level):
OldboyPeople.__init__(self, name, age, sex)
self.level = level
self.courses = [] def score(self, stu, num):
stu.score = num def tell_all_course(self):
print(('老师[%s]教授的课程如下' % self.name).center(50, '*'))
for obj in self.courses:
obj.tell_info()
print('*' * 70) class Course:
def __init__(self, c_name, c_price, c_period):
self.c_name = c_name
self.c_price = c_price
self.c_period = c_period def tell_info(self):
print('<课程名:%s 价钱:%s 周期:%s>' % (self.c_name, self.c_price, self.c_period)) # 创建课程对象
python = Course('python全栈开发', 1900, '5mons')
linux = Course('linux架构师', 900, '3mons') stu1 = OldboyStudent('刘二蛋', 38, 'male')
stu1.courses.append(python)
stu1.courses.append(linux)
stu1.tell_all_course() tea1 = OldboyTeacher('egon', 18, 'male', 10)
tea1.courses.append(python)
tea1.tell_all_course()
面向对象-多态与多态性
1.什么是多态
多态指的是同一种/类事物的不同形态
2.为何要用多态
多态性:在多态的背景下,可以在不用考虑对象具体类型的前提下而直接使用对象
3.如何用多态
Animal() #父类只是用来建立规范的,不能用来实例化,更无需实现内部的方法
import abc class Animal(metaclass=abc.ABCMeta):
@abc.abstractmethod
def speak(self):
pass @abc.abstractmethod
def run(self):
pass class People(Animal):
def speak(self):
print('say hello') def run(self):
pass class Dog(Animal):
def speak(self):
print('汪汪汪') def run(self):
pass class Pig(Animal):
def speak(self):
print('哼哼哼') def run(self):
pass obj1 = People()
obj2 = Dog()
obj3 = Pig()
Python推崇的是鸭子类型,只要你叫的声音像鸭子,并且你走路的样子也像鸭子,那你就是鸭子
class Disk:
def read(self):
print('Disk read') def write(self):
print('Disk write') class Memory:
def read(self):
print('Mem read') def write(self):
print('Mem write') class Cpu:
def read(self):
print('Cpu read') def write(self):
print('Cpu write') obj1 = Disk()
obj2 = Memory()
obj3 = Cpu() obj1.read()
obj2.read()
obj3.read()
面向对象-封装
1.什么是封装
装: 往容器/名称空间里存入名字
封: 代表将存放于名称空间中的名字给藏起来,这种隐藏对外不对内
2.为何要封装
封装数据属性:
将数据属性隐藏起来,类外就无法直接操作属性,需要类内部开辟一个接口,让外部的使用可以间接地操作属性,可以在接口内定制任意的控制逻辑,从而严格控制使用者对属性的操作
class People:
def __init__(self, name, age):
self.__name = name
self.__age = age def tell_info(self):
print('<name:%s age:%s>' % (self.__name, self.__age)) def set_info(self, name, age):
if type(name) is not str:
print('名字必须是str类型傻叉')
return
if type(age) is not int:
print('年龄必须是int类型傻叉')
return
self.__name = name
self.__age = age obj = People('egon', 18)
obj.set_info('EGON', '')
obj.tell_info()
封装函数属性: 隔离复杂度
class ATM:
def __card(self):
print('插卡') def __auth(self):
print('用户认证') def __input(self):
print('输入取款金额') def __print_bill(self):
print('打印账单') def __take_money(self):
print('取款') def withdraw(self):
self.__card()
self.__auth()
self.__input()
self.__print_bill()
self.__take_money() a = ATM()
a.withdraw()
3.如何封装
在类内定义的属性前加__开头(没有__结尾)
总结:
1. __开头的属性实现的隐藏仅仅只是一种语法意义上的变形,并不会真的限制类外部的访问
2. 该变形操作只在类定义阶段检测语法时发生一次,类定义阶段之后新增的__开头的属性并不会变形
3. 如果父类不想让子类覆盖自己的属性,可以在属性前加__开头
class Foo:
def __f1(self): # _Foo__f1
print('Foo.f1') def f2(self):
print('Foo.f2')
self.__f1() # obj._Foo__f1() class Bar(Foo):
def __f1(self): # _Bar__f1
print('Bar.f1') obj = Bar() >>>Foo.f2
>>>Foo.f1
面向对象-property
property装饰器是用来将类内的函数属性伪装成数据属性
class People:
def __init__(self, name):
self.__name = name @property
def name(self):
return '<名字:%s>' % self.__name @name.setter
def name(self, obj):
if type(obj) is not str:
print('name必须为str类型')
return
self.__name = obj @name.deleter
def name(self):
print('不让删') obj = People('egon')
del obj.name
print(obj.__dict__)