python interface class, an abstract class, polymorphic

Reference https://www.cnblogs.com/Fantac/p/11481114.html

 

Interfaces and abstract classes class summary

No interface class in python, but abstract classes; ABC module metaclass = ABCMeta, @ abstractmethod its essence is to do with the specification of the code, the class name of the desired parent exactly the same way to achieve subclass

Interfaces and abstract classes:

Java from the point of view there is a difference, java already supports single inheritance so there is an abstract class; java no multiple inheritance, so in order to isolate the interface principle, this concept design interface, support for multiple inheritance of

The python It supports single inheritance also supports multiple inheritance, so the difference between abstract class and interface class would not be so obvious, even built-in interface class in python

 

Polymorphism

Polymorphism refers to a class of things have a variety of forms

natural python support polymorphism

from abc Import AbstractMethod, ABCMeta
 class Payment (the metaclass that = ABCMeta): # Payment class
    @abstractmethod
    def pay(self,money):
        pass

class wechat (Payment): # Payment --- micro-channel pay
    def pay(self,money):
        Print ( ' has been paid by the micro-channel element% s ' % Money)
 class Alipay (Payment): # --- payment Alipay
    def pay(self,money):
        Print ( ' has been used Alipay paid RMB% s ' % Money)
 class the Apple (Payment): # Payment --- Apple pay
    def pay(self,money):
        Print ( ' it has been used for a Alipay membered% s ' % Money)

Polymorphism

That is, although it is a kind of thing but they perform the same method but do different things, such as micro-channel it uses micro-channel payment, Alipay it uses Alipay, apple pay for it with a Mac pay ...

Ducks type

python language which has a duck type concept, and in some other part of a strongly typed language, using the polymorphic

Ducks type: according to income is not advocating inherited similar, I just realized I myself own code on it, if there are just two classes are similar, does not produce brotherhood subclasses of the parent class, and that this is the type of duck . Like list, tuple this similarity, it is write the code itself bound, rather than by the constraints of the parent class

Guess you like

Origin www.cnblogs.com/wind666/p/11961899.html