python's twenty-first day for me abstract and interface classes and polymorphism

Normalized design:

    No matter which class the object is, it calls the same function to accomplish similar functions.

class Alipay:
     def pay(self,money):
         print ( ' Used Alipay to pay %s ' % money)

class QQpay:
     def pay(self,money):
         print ( ' Use QQ to pay %s yuan ' % money)

def pay(way,money):    #The   above two classes both use the pay method to complete similar functions,                       
    way.pay(money) #So you     can define a ***same function name*** to complete this function, This is called a normalized design. 

a = Alipay()
q = QQpay()
pay(a, 100)       #paid 100 with Alipay 
pay(q,100) #paid 100       with QQ

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; details should depend on abstractions. In other words, program for the interface, not for the implementation.

Abstract class: It is a specification for defining classes in python.

    If you encounter an abstract class, remember to implement the corresponding methods one by one according to the specifications in the abstract class.

  Abstraction is to extract similar or more similar parts. The main function of abstraction is to divide categories. In the process of abstract knowledge analysis and design, an action or a skill can be obtained through abstraction. Abstraction is a special class that can only be inherited and cannot be instantiated.

  The essence of an abstract class is still a class, which refers to the similarity of a group of classes, including data attributes and function attributes.

Interface class:

  Interface classes only emphasize the similarity of function attributes, and there is no such concept as interface classes in python.

  The concept of interface class comes from JAVA,

    Java classes do not have multiple inheritance, so using interfaces, interfaces can implement multiple inheritance.

  Interface isolation principle:

      Use multiple specialized interfaces instead of a single overall interface. 

An interface extracts a group of common functions, and an interface can be regarded as a collection of functions.

Then let the subclass implement the functions in the interface.

The meaning of this is normalization. What is normalization is that as long as the classes are implemented based on the same interface, the objects generated by all these classes will be the same in terms of usage when they are used.

Normalization allows users not to care about the class of objects, but only to know that these objects have certain functions, which greatly reduces the difficulty for users to use.

For example: we define an animal interface, which defines interface functions such as running, eating, breathing, etc., so that the mouse class implements the interface, and the squirrel class also implements the interface, and the two generate a mouse respectively. And a squirrel brought to you, even if you can't tell which one is which, you know for sure that they can both run, eat, and breathe.

Another example: we have a car interface, which defines all the functions of the car, and then the Honda car class, the Audi car class, the Volkswagen class, they all implement the car interface, so it's easy to handle, everyone only needs If I learned how to drive a car, then whether it is Honda, Audi, or Volkswagen, we will drive it. When driving, I don't need to care what kind of car I drive, and the operation method (function call) is the same.
Why use interface classes

 

# DescriptionZoo 
# can swim, walk, climb trees, fly 
# tiger 
# frog 
# swan 
# monkey


#All flying animals have some characteristics of flying animals # All walking animals have some characteristics of walking animals

from abc import ABCMeta,abstractmethod
class FlyAnimal(metaclass=ABCMeta):
    @abstractmethod
    def fly(self):pass
    @abstractmethod
    def cal_fly_speed(self):pass
    @abstractmethod
    def cal_fly_height(self):pass
class WalkAnimal(metaclass=ABCMeta):
    @abstractmethod
    def walk(self):pass
class SwimAnimal(metaclass=ABCMeta):
    @abstractmethod
    def swim(self):pass
class Tiger(WalkAnimal,SwimAnimal):
    def walk(self):pass
    def swim(self):pass
class Monkey(WalkAnimal):
    def walk(self):pass
    def climb(self):pass
class Swan(FlyAnimal,WalkAnimal,SwimAnimal):
    def swim(self):pass
    def walk(self):pass
    def fly(self):pass
    def cal_fly_speed(self):pass
    def cal_fly_height(self):pass

 

The role of the interface class:

    In java, the principle of interface isolation can be satisfied, and the constraints of multiple inheritance can be completed.

   In python, the principle of interface isolation is satisfied. Since python itself supports multiple inheritance, the concept of interface is not needed.

What abstract classes and interface classes do: establish specifications, and formulate a class whose metaclass is ABCMeta, then this class becomes an abstract class (interface class). The main function of this class is to establish a specification . The methods decorated by abstractmethod must be implemented by inherited subclasses , otherwise an error will be reported during the instantiation phase.

from abc import ABCMeta,abstractmethod
 class Payment(metaclass=ABCMeta): #Abstract      class interface class specification and constraints metaclass specifies a metaclass 
    @abstractmethod
     def pay(self,money): pass       #Abstract method specifies the word class under this class Both must have the method name pay.

class Alipay(Payment): #Inherit    Payment def 
    pay (self,money): #The         method name must be pay 
        print ( ' Use Alipay to pay %s yuan ' % money)

class QQpay(Payment): #Inherit    Payment def 
    pay (self,money):         #The method name must be pay 
        print ( ' Use QQ to pay %s yuan ' % money)

class Wechatpay(Payment): #Inherit    Payment def 
    pay (self,money):         #The method name must be pay 
        print ( ' Wechat paid %s yuan ' % money)

a = Alipay()
q = QQpay()
w = Wechatpay ()
a.pay( 100)       #paid 100 yuan using Alipay 
q.pay(100) #paid       100 yuan using QQ 
w.pay(100) #paid       100 yuan using WeChat

Summary: Neither abstract classes nor interface classes metaclass = ABCMeta can be instantiated.

Abstract classes and interface classes:

   In python:

    1, there is no difference, they are all 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, multiple interface classes should be used for separate constraints.

In java:

  1. Abstract classes and interfaces are very different.

  2. The essence of an abstract class is still a class. If it is a class, it must follow the rules of single inheritance. Therefore, if a subclass is constrained by an abstract class, it can only 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:

  java c++ c# ----- Strongly typed language. The incoming parameter must indicate what type of parameter it is.

    Such as: def func(int a) : pass

  Perform operations between the same data types.

  Shell language ----- Weakly typed language. For example: you can directly calculate 1+'1'

  Between strongly typed and weakly typed ----- python dynamic strongly typed language.

  Perform operations between types of the same data.

Java polymorphism: Objects of multiple classes developed under a class can be passed in as parameters.

Both python2x and python3x are inherently polymorphic.

Polymorphism and duck typing:

    Polymorphism is achieved through inheritance:

      Various types of objects developed by java under a class can be passed into a function or method as a parameter.

      在python中不需要可以实现多态,因为python本身自带多态效果。

   鸭子类型:

      不是通过具体的继承关系来约束某些类中必须有哪些方法名。

      是通过一种约定俗成的概念来保证在多个类中相似的功能叫相同的名字。

Guess you like

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