Polymorphism and abstract classes

abstract classes and interface classes

What Abstract and Interface Classes Do: Establishing Specifications

open closed principle

​ Open: open to extension

​ Closed: closed to modification

Normalized design:

​ No matter which class of object, call a function to complete the same function

​ len() and _ _ len__ (), only one class that implements the _ _ len__() method can use the len() function

 

  
from abc import ABCMeta, abstractmethod #The
established metaclass is ABCMeta, then this class becomes an abstract class (interface class), and the main function of this class is to establish a specification
#All methods decorated by abstractmethod in the abstract class must be The inherited subclass implements it. If it is not implemented, an error will be reported during instantiation.
# Neither abstract classes nor interface classes metaclass = ABCMtea can be instantiated
  Dependency inversion principle: 
high-level modules should not depend on low-level modules, both should depend on their abstractions; abstractions should not depend on details, and details should depend on abstractions; in other words, program for interfaces, not implementations.

Interface isolation principle:

Use multiple dedicated interfaces instead of a single overall interface

The role of the interface:

​ In Java, the interface isolation principle can be completed, and multiple inheritance constraints can be completed. In python, the interface isolation principle can be satisfied. Since python itself supports multiple inheritance, the concept of interfaces is not needed.

Abstract class and interface class: (there is no concept of interface in python)

​ 1. In python:​ 1. It is no different in python, it is used to constrain the methods in the subclass

​ 2. As long as the methods decorated by abstractmethod in abstract classes and interface classes need to be implemented by subclasses​ 3. It should be noted that when multiple classes have the same function and different functions, they should be implemented by subclasses. Use interface classes to constrain separately

​ 2. In Java:​ 1. Abstract classes and interfaces are completely different​ 2. The essence of an abstract class is still a class, and a class must follow the rules of single inheritance, so if a subclass is constrained by an abstract class, then she only Can be controlled by a parent class​ 3. When multiple classes have the same function and different functions, java can only use the interface to solve the problem

Polymorphism and duck typing

Polymorphism: Objects of multiple classes developed under one class can be passed in here as parameters

Python is born with polymorphic effects

1 < 1 > Polymorphism is achieved through inheritance (subclass can be used as parent class)
 2 < 2 > Subclass achieves polymorphism by overloading the method of parent class
 3  
4  class Animal:
 5    def move(self):
 6      print( ' animal is moving.... ' )
 7  class Dog(Animal):
 8    pass
 9  def move(obj):
 10    obj.move()
 11  
12 >>> move(Animal())
 13 >>>animal is moving ....
 14 >>> move(Dog())
 15 >>> animal is moving....
16 
17 class Fish(Animal):
18   def move(self):
19     print('fish is moving....')
20 >>>move(Fish())
21 >>>fish is moving....

 

duck type

1 < 1 > The type of variable binding is indeterminate
 2 < 2 > Functions and methods can receive parameters of any type
 3 < 3 > The provided parameter type is not checked when the method is called
 4 < 4 > The call to a method with parameters is successful And the attribute is determined, the call is unsuccessful, an error is thrown
 5 < 5 > No need to implement the interface
 6  
7  class P:
 8    def __init__(self, x, y):
 9      self.x = x
 10      self.y = y
 11    def __add__( self, oth):
 12      return P(self.x+oth.x, self.y+ oth.y)
 13   def info(self):
14     print(self.x, self.y)
15 class D(P):
16   def __init__(self, x, y, z):
17     super.__init__(x, y)
18     self.z = z
19 
20   def __add__(self, oth):
21     return D(self.x+oth.x, self.y+oth.y, self.z+oth.z)
22   def info(self):
23     print(self.x, self.y, self.z)
24 
25 class F:
26   def __init__(self, x, y, z):
27     self.x = x
28     self.y = y
29     self.z = z
30 
31   def __add__(self, oth):
32     return D(self.x+oth.x, self.y+oth.y, self.z+oth.z)
33   
34   def info(self):
35     print(self.x, self.y, self.z)
36   
37 
38 def add(a, b):
39   return a + b
40 
41 if __name__ == '__main__':
42   add(p(1, 2), p(3, 4).info())
43   add(D(1, 2, 3), D(1, 2, 3).info())
44   add(F(2, 3, 4), D(2, 3, 4).info())

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324520537&siteId=291194637