And a combination of polymorphism (parent class inheritance, abstract classes, duck type)

[TOC]

combination

1. What is a combination

Refers to a combination of an object has an attribute value of the property is another object of the class

 class Foo:
        pass
    class Bar:
        pass
    obj=Foo()
    obj.attr=Bar()

    obj.xxx
    obj.attr.yyy

2. Why do you use a combination of

通过为某一个对象添加属性(属性的值是另外一个类的对象)的方式,可以间接地将两个类关联/整合/组合到一起,从而减少类与类之间代码冗余
 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. Use a combination of scenario

组合与继承都是有效地利用已有类的资源的重要方式。但是二者的概念和使用场景皆不同,
1.继承的方式
通过继承建立了派生类与基类之间的关系,它是一种'是'的关系,比如白马是马,人是动物。
当类之间有很多相同的功能,提取这些共同的功能做成基类,用继承比较好,比如老师是人,学生是人

2.组合的方式
用组合的方式建立了类与组合的类之间的关系,它是一种‘有’的关系,比如教授有生日,教授教python和linux课程,教授有学生s1、s2、s3...
当类之间有显著不同,并且较小的类是较大的类所需要的组件时,用组合比较好

4. The combination of Example

class People:
    school = 'Oldboy'

    def __init__(self,name,age,sex,):
        self.name = name
        self.age = age
        self.sex = sex

class Student(People):
    def __init__(self, name, age, sex,score=0):
        People.__init__(self,name,age,sex)
        self.score = score

    def choose_course(self):
        print('%s choosing course' % self.name)

class Teacher(People):
    def __init__(self,name,age,sex,level):
        People.__init__(self,name,age,sex)
        self.level=level

    def score(self,stu,num):
        stu.score=num

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=Student('刘二蛋',38,'male')
stu1.course=python
# print(stu1.__dict__)
stu1.course.tell_info()

tea1=Teacher('egon',18,'male',10)
tea1.course=python
# print(tea1.__dict__)
tea1.course.tell_info()

Polymorphism

1. Polymorphic definition:

Polymorphism refers to the various forms of the same thing

2. polymorphic object:

Polymorphic also known as polymorphism, inheritance, polymorphism is the manifestation in the program. Polymorphic purpose is to allow a variety of different types of objects, in the case where the same function (method), can respond differently. Parent class: the definition of a uniform set of criteria, sub-categories: follow uniform standards parent. Polymorphic ultimate goal: a unified subclasses preparation of specifications, in order to allow users to more easily call the same function method. The essence of polymorphism lies in unity.

3. How to achieve polymorphism

1. inherit the parent class

class Animal:  # 父类定义一套统一的标准,不是为了实现某一具体功能,具体实现功能还是要继承的那些子类,这样使用者只需要学习父类的一套标准就行了。
    def eat(self):
        pass
    def drink(self):
        pass
    def speak(self):
        pass
        # print('我们一起喵喵喵。。。。')

# 猪
class Pig(Animal):
    # 吃
    def eat(self):
        print('猪在吃饭')
        pass
    # 喝
    def drink(self):
        pass
    def speak(self):
         # super().speak()
        print('哼哼哼~~~')
# 猫
class Cat(Animal):
    # 吃
    def eat(self):
        print('猫在吃饭')
       
    # 喝
    def drink(self):
        pass
    def speak(self):
        print('喵喵喵~~')
# 狗
class Dog(Animal):
    # 吃
    def eat(self):
        print('狗在吃饭')
    # 喝
    def drink(self):
        pass
    def speak(self):
        print('汪汪汪~~~')
        
pig = Pig()
cat = Cat()
dog = Dog()

pig.speak()
cat.speak()
dog.speak()

2. abstract class

Note that in Python is not mandatory to follow a standard set of subclasses of the parent class, so if you want to force subclasses to follow a standard set of parent class, then they would use an abstract class. 1. What is? Importing purpose abc (abstract_class) Module 2 used? The subclass must follow a set of mandatory standards of the parent class 3. How to use? import abc

import abc
class Animal(metaclass=abc.ABCMeta):
    # 吃
    @abc.abstractmethod
    def eat(self):
        pass
    # 喝
    @abc.abstractmethod
    def drink(self):
        pass
    # 叫
    @abc.abstractmethod
    def speak(self):
        pass
# Animal() # 父类只是用来建立规范的,不能用来实例化,更无须实现内部的方法。会报错
# 猪
class Pig(Animal):  # 子类在继承父类时,就必须遵循父类制定的规范,即遵守父类内部定义的抽象类方法,否则就报错
    # 吃
    def eat(self):
        print('猪在吃饭')
        pass
    # 喝
    def drink(self):
        pass

    def speak(self):
        print('哼哼哼~~~')

    # 派生
    def run(self):
        pass

pig = Pig()

3. Duck type (not recommended mandatory follow the standard parent class)

Ducks Type: without knowing the current object is the thing of the situation, but you look like a duck, then you are a duck. Note: In Python, not mandatory subclass must follow a standard set of the parent class, so there are a type of duck.

# 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()

4. The realization of the advantages and disadvantages of the three ways polymorphic

Inheritance (or parent class inherit the abstract class): coupling is too high, poor scalability; duck Type: low coupling, strong scalability program;

Guess you like

Origin www.cnblogs.com/zhangchaocoming/p/11666707.html