Abstract classes and polymorphism

1. Abstract classes and interface classes

Inheritance serves two purposes:

One: Inherit the methods of the base class and make your own changes or extensions (code reuse)  

Two: declare that a subclass is compatible with a base class, define an interface class Interface, the interface class defines some interface names (that is, function names) and does not implement the function of the interface, the subclass inherits the interface class, and implements the interface class. Function

In practice, the first meaning of inheritance is not very meaningful, and is often even harmful. Because it makes the subclass strongly coupled to the base class.

The second meaning of inheritance is very important. It is also called "interface inheritance".
Interface inheritance essentially requires "to make a good abstraction that specifies a compatible interface, so that external callers do not need to care about specific details, and can treat all objects that implement a specific interface equally" - this is in programming design. , called normalization.

Normalized design: no matter which class object, call the same function

from abc import ABCMeta, abstractmethod


class Payment(metaclass=ABCMeta): #Abstract   class or interface class, as a specification or constraint, metaclass specifies a metaclass 
    def pay(self): pass


class Alipay(Payment):
     def pay(self, money):
         print ( " Payment of %s yuan using Alipay " % money)


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


class Wechatpay(Payment):
     def pay(self, money):
         print ( " Used WeChat to pay %s yuan " % money)


def pay(s, money):   #Normalized design, no matter what class of object it is, call the same function to complete similar functions 
    s.pay(money)


a = Alipay()
a.pay(100)
pay(a, 100)
View Code

What abstract classes and interface classes do: establish specifications If
  the metaclass of a class is ABCMeta,
  then this class becomes an abstract class (interface class)
  The main function of this class is to establish a specification

All methods decorated by abstractmethod in abstract classes must be implemented by inherited subclasses.
  If they are not implemented, an error will be reported during the instantiation phase.

Neither abstract classes nor interface classes (metaclass=ABCMeta) can be instantiated

The abstract class

What is an abstract class

    Like java, python also has the concept of abstract class, but it also needs to be implemented with the help of modules. An abstract class is a special class. Its special feature is that it can only be inherited and cannot be instantiated.

Why have abstract classes

    If a class is said to extract the same content from a bunch of objects , then an abstract class is to extract the same content from a bunch of classes , including data attributes and function attributes

From a design point of view, if the class is abstracted from the real object, then the abstract class is based on the class abstraction.

From the perspective of implementation, the difference between an abstract class and an ordinary class is that there are abstract methods in an abstract class, the class cannot be instantiated, but can only be inherited, and subclasses must implement abstract methods

The essence of an abstract class is still a class, which refers to the similarity of a group of classes, including data attributes (such as all_type) and function attributes (such as read, write), while the interface only emphasizes the similarity of function attributes.

Abstract class is a direct concept between class and interface. It has some characteristics of class and interface at the same time, and can be used to realize normalized design. 

In python, there is no such thing as an interface class. Even if the interface is not defined through a special module, we should have some basic concepts.

1. Multiple inheritance problem

In the process of inheriting abstract classes, we should try to avoid multiple inheritance;
when inheriting interfaces, we encourage you to inherit multiple interfaces instead.

2. Implementation of the method

In abstract classes, we can make basic implementations of some abstract methods;
in interface classes, any method is just a specification, and specific functions need to be implemented by subclasses

3. Interface isolation

Interface: The interface extracts a group of common functions, and the interface can be regarded as a collection of functions.

Then let the subclass implement the functions in the interface.

The significance 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 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.

The role of interface class:
  In Java, it can satisfy the principle of interface isolation and complete the constraints
  of multiple inheritance. In python, it can satisfy the principle of interface isolation. Since python itself supports multiple inheritance, the concept of interface is not needed.

Abstract class and interface class:
in python:
  there is no difference in python, they are all used to constrain the methods in subclasses.
  As long as abstract classes and interface classes are decorated with abstractmethod, they all need to be implemented by subclasses
  . Yes, when multiple classes have the same function and different functions, multiple interface classes should be used to carry out separate constraints.
In Java:
  abstract classes are completely different from interfaces
  . The rule of single inheritance must be followed, so if a subclass is constrained by an abstract class, it can only be controlled by a parent class.
  When multiple classes have the same function and different functions, Java can only use interfaces to solve the problem question

Abstract class: It is a specification for defining classes in python, and the corresponding methods are implemented one by one according to the specifications in the abstract class

Four, python programming principles

1. The principle of open and closed:

  Open: open to extension

  closed: closed to modification

2. 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

3. Interface isolation principle: 

  Use multiple specialized interfaces instead of a single overall interface. That is, clients should not rely on those interfaces that are not needed.

5. Polymorphism

Strongly typed language: Java c++ c# # Do operations between the same data type, the data type must be specified, so Java needs to declare a superclass first, and use the superclass as the data type

Weakly typed languages: the shell
is somewhere in between: python # Dynamically strongly typed languages 

Polymorphism and duck type 
polymorphism implement
java through inheritance. Objects of multiple classes developed under a class can be passed into a function or method as a parameter.
In python, there is no need to deliberately implement polymorphism, because python itself has many Dynamic effect
duck type
It is not through a specific inheritance relationship to constrain which method names must be in certain classes, but
through a conventional concept to ensure that similar functions in multiple classes have the same name

Guess you like

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