Python polymorphism and abstract classes

A polymorphic

1.1 What is a multi-state

Polymorphism, also known as "polymorphism" refers to the same type of thing, different forms.
In python polymorphism were referring to many types of data, if have similar properties and methods properties are unified good naming convention, this can increase the uniformity of code developers, making it easier to understand the caller.

Over 1.2 Objective states:

To without knowing the specific type of the object, call the method of unified specification objects (name)

Parent Class: customize a unified standard. (Example: a unified method name)
sub-categories: standard and consistent parent class. (Example: the subclass follow a uniform method name of the parent class)

Polymorphism of three forms :

  1. Inheritance abstract class
    - a high degree of coupling, scalability, low procedures
  2. Inherited from the parent class
    - coupling high, low scalability program
  3. Ducks type
    - Low coupling, high scalability program
#动物类
class Animal:
    #方法:吃
    def eat(self):
        pass
    #方法:叫
    def speak(self):
        pass

#猪类
class Pig(Animal):
    def eat(self):
        print('bia唧')
    def speak(self):
        print('哼哼哼')

#猫类
class Cat(Animal):
    def eat(self):
        print('咬...')
    def speak(self):
        print('喵喵喵')
#狗类
class Dog(Animal):
    def eat(self):
        print('叫...')
    def speak(self):
        print('汪汪汪')
        
pig1 = Pig()
cat1 = Cat()
dog1 = Dog()

# 设置一个接口
def SPEAK(obj):
    obj.speak()

SPEAK(pig1)     #pig1.speak()
SPEAK(cat1)     #cat1.speak()
SPEAK(dog1)     #dog1.speak()

The benefits of more than 1.3 states:

1. increase the flexibility of the program of
  maintaining the status quo, regardless of the ever-changing target user is the same kind of form to call, such as func (animal)

2. increase the scalability of the program
  created by animal class inherits a new class, users do not need to change your code, or use func (animal) to call  

class People(Animal): #属于动物的另外一种形态:人
    def speak(self):
        print('say hi')
 
def func(people): #对于使用者来说,自己的代码根本无需改动
    people.speak()
    
people1=People() #实例出一个人
func(people1) #甚至连调用方式也无需改变,就能调用人的talk功能
'''
这样我们新增了一个形态People,由People类产生的实例people1,使用者可以在完全不需要修改自己代码的情况下,
使用和猫、狗、猪一样的方式调用people1的speak方法,即func(people1)
'''

Second, the abstract class

2.1 What is an abstract class?

Abc built-in python module, there is an abstract class, so that subclasses must follow the specifications written in the parent class.

2.2 How to implement an abstract class

  • Abc needs to inherit parent module metaclass = abc.ABCMeta
  • In the method of the parent class, the required decorative abc.abstractmethod

note:

  1. We do not recommend the use of abstract classes in python.
  2. Subclasses must write specification method according to the parent class, are indispensable. (As long as there are several methods abstract parent class, subclass it is necessary to define a few, can be more, but no less)
import abc
#动物类
class Animal(metaclass = abc.ABCMeta):
    #方法:吃
    @abc.abstractmethod
    def eat(self):
        pass
    #方法:叫
    @abc.abstractmethod
    def speak(self):
        pass

#猪类
class Pig(Animal):
    def eat(self):
        print('bia唧')

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


pig1 = Pig()
pig1.eat()

Third, the type of duck

3.1 What is type duck

python advocating duck type, different objects, just looks like a duck and acts like a duck action, then it is a duck.
Duck type is a form of polymorphism.

3.2 Why should duck type

Different objects, to abstract the same type of approach, giving them a uniform set of custom specifications. All classes are conducted when defining prepared in accordance with uniform standards.

#二者都像鸭子,二者看起来都像文件,因而就可以当文件一样去用
class TxtFile:
    def read(self):
        pass

    def write(self):
        pass

class DiskFile:
    def read(self):
        pass
    def write(self):
        pass
序列类型:list,str,tuple

l = lisst([1,2,3])
s = str('hello')
t = tuple((1,2,'a'))

#我们可以在不考虑三者类型的前提下使用s,l,t
l.__len__()
s.__len__()
t.__len__()
str1 = 'bh'
list1 = [1,2,3]

# print(str1.__len__())
# print(list1.__len__())

# 自定义统计长度函数
def LEN(obj):
    return obj.__len__()

print(LEN(str1))
print(LEN(list1))

Guess you like

Origin www.cnblogs.com/baohanblog/p/12143071.html