abstract class interface class

 

 

What Abstract and Interface Classes Do: Establishing Specifications

The metaclass of a class is ABCMeta, then this class is programmed with an abstract class (interface class). The function of this class is to create a specification class so that the specifications in it have the same methods or attributes.

All methods decorated by abstractmethod in abstract classes must be implemented by inherited subclasses

If it is not implemented, an error will be reported during the instantiation phase

 

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

 

 

from abc import ABCMeta, abstractmethod  
 class Payment(metaclass = ABCMeta): #This is to create a canonical class
    @abstractmethod
    def pay(self, money):
         print ( ' recharged %s yuan ' % money)

class Person(Payment):
     def pay(self,money):
         print ( ' recharged %s yuan ' % money)
 def pay(a, money):
     print ( ' recharged %s yuan ' % ( money))

alex = Person()
# alex.pay(1000)
pay(alex,1000)

What is an interface?

Python does not have the concept of interface, so where does the concept of interface come from?

  Java classes do not have multiple inheritance, interfaces can implement multiple inheritance

The role of the interface class:

  In Java, it can meet the interface isolation principle and complete the constraints of multiple inheritance,

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

abstract classes and interface classes

In python:

  There is no difference, they are all used to constrain the methods in the word class

  As long as the methods decorated by abstractmethod in abstract classes and interface classes need to be implemented by word classes

  It should be noted that when multiple word classes have the same function and different functions, multiple interface classes should be used to constrain (at this time, I feel that different constraint classes are defined to constrain different word classes)

Abstract class and interface class are completely different in java

  The essence of an abstract class is that a class is a class and must follow the rules of single inheritance, so a word class is constrained by an abstract class, so 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

 

When interviewing:

  Abstract class: a specification for defining classes in python

When writing code in a company:

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

 Objects of multiple classes developed by java polymorphism under one class can be passed in here as parameters

Whether python2 or python3 is inherently polymorphic, it should inherit the object class by default

The data type of an object is the class in which it resides

 

#Duck type # class QQpay(): 
#      def pay(self,money): 
# 
print          ('Use qq to pay %s yuan'%money)
#
# class Wechatpay(): 
#      def pay(self,money): 
#          print('Used WeChat to pay %s yuan'%money)
#
# def pay(pay_obj,money):
#     pay_obj.pay(money)

Polymorphism and duck typing
# Polymorphism is implemented through inheritance
# Java Objects of multiple classes developed under a class can be passed into a function or method as a parameter
# There is no need to deliberately implement polymorphism in python, because python itself Self-contained polymorphic effect
# Duck type
# It is not through specific inheritance relations to constrain which method names must be in certain classes
# It is through a conventional concept to ensure that similar functions in multiple classes are called the same name

 

Guess you like

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