Abstract classes and interfaces

Interfaces and abstract classes (both an ideological concept)

A simple explanation of what is an abstract class and interface class

  Java is mainly object-oriented programming, more respected design patterns, and the interface is a conceptual thinking in design mode inside, so the interface class is Java which natively supported, and the python natively does not support the interface class, but because of the design patterns inside this concept has an interface class, and will be used python design patterns of thinking, it will also come into contact with the interface class.

Interfaces: python does not support native

Abstract class: python native support

 

Second, the code which implements the interface class

Several definitions of payment, and finally unified payment entrance, the code is as follows:

class Wechatpay:
     DEF Pay (Self, Money):
         Print ( ' have used micro-channel paid% s Element ' % Money) 

class Alipay:
     DEF Pay (Self, Money):
         Print ( ' has been used Alipay the% s Element ' % Money) 

# then define a Applepay class 
class Aapplepay:
     DEF fuqian (Self, Money):   # this is not to use pay function 
        Print ( ' already using Apple paid a% s Element ' % Money) 

DEF pay (pay_obj, Money):
     # unity pay entrance Either way, mainly to pay for it
    pay_obj.pay (Money) 

WeChat = Wechatpay ()
 # instantiated 
Ali = Alipay ()
 # instantiated 
App = Aapplepay ()
 # instantiated 
Pay (WeChat, 100)   # payment 100 through micro channel 
Pay (Ali, 200 is)   # Alipay pay 200 
pay (App, 300)   # here pay function calls, but there are not using front Applepay pay function but fuqian, so this error will result

operation result:

C: \ Users \ sku1-1 \ PycharmProjects \ untitled \ venv \ Scripts \ python.exe C: / Users / sku1-1 / PycharmProjects / untitled / study notes / interfaces and classes .py abstract classes 
have been used to pay $ 100 micro letter 
has been used Alipay pay 200 yuan 
Traceback (MOST recent Results Call Last): 
  File " <encoding error> " , Line 26, in <Module> 
  File " <encoding error> " , Line 16, in the pay 
AttributeError: ' Aapplepay ' Object has attribute NO ' Pay '

 

  From the above operating results may know, the third payment defined, can not be paid through a unified payment after the entrance, the result will be an error, an error to change the way the following code:

# Then define a class 
class Payment:
     DEF pay (Self, Money):
         The raise NotImplementedError   # reportedly did not realize the error of this method, it is possible to know the wrong front where they should exist and methods pay the same name as the 

class Wechatpay (Payment):   # inheritance payment class, but first find within himself whether there is pay function, have to use their own, not only to find the father 
    DEF pay (Self, Money):
         Print ( ' have used micro-channel pay% s Element ' % Money) 

class Alipay ( payment):
     DEF pay (Self, Money):   # inherit payment class, but first find within himself whether there is pay function, have to use their own, not only to find the father 
        Print ( ' has been used Alipay the% s Element ' % Money) 

#And then define a class Applepay 
class Aapplepay (Payment):
     # inherit Payment class, but first find out whether there is pay yourself inside a function, have to use their own, only to find no parent, because there is no pay function, 
    # so run a parent , the final report will 'NotImplementedError' error 
    DEF fuqian (Self, Money):   # this is not to use pay function 
        Print ( ' already using Apple paid RMB% s ' % Money) 

DEF pay (pay_obj, Money):
     # unity pay entrance Either way, it is mainly paid 
    pay_obj.pay (Money) 

wechat = Wechatpay ()
 # instantiated 
Ali = Alipay ()
 # instantiated 
App = Aapplepay ()
 # instantiated 
pay (wechat, 100 )  # Payment through micro-channel 100 
pay (Ali, 200)   # payment 200 through Alipay 
pay (App, 300)   # calls pay function here, but inside in front of Applepay not using a pay function but fuqian, so this error will result

operation result:

Have used micro-channel pay $ 100 
Traceback (most recent call last): 
has been used Alipay $ 200 
  File " <encoding error> " , Line 33 is, in <Module1> 
  File " <encoding error> " , Line 23 is, in Pay 
  File " <encoding error> " , Line 4, in the Pay 
NotImplementedError

  We can see from the code, as long as you pay payment method inside a function does not exist, it will run a parent payment method directly inside the pay, which directly report the error to write their own back, which can prompt payment errors which should exist

And pay the same method in the parent class method or function, but this must be an error method to find the error after the call through the pay method, in the case if you want to call the method does not pay the wrong place can be found, such Code improvements are as follows:

from abc Import AbstractMethod, ABCMeta
 class Payment (the metaclass that = ABCMeta):   # default metaclass of the type 
    @abstractmethod
     DEF the Pay (Self, Money):
         Pass 
# Just use the above method to show that such a norm has been established, all of the following specification the code, the code makes the following methods and should pay the same name exists, 
# does not exist can not be, it will error, and error method also specifically pointed out where the error 

class Wechatpay (Payment):   # inherit Payment class, but first find yourself whether there are pay function, have to use their own, not only to find parent 
    DEF pay (Self, Money):
         Print ( ' have used micro-channel paid% s element ' % Money) 

class Alipay (payment):
     DEF pay (Self , Money):   #Inheritance Payment class, but first find out if they are there pay function, have to use their own, not only to find the father 
        Print ( ' has been used Alipay the% s Element ' % Money) 

# then define a Applepay class 
class Aapplepay (Payment ):
     # inherit Payment class, but first find out whether there are pay its own function, have to use their own, only to find no parent, because there is no pay function, 
    # so run a parent, the final report will 'NotImplementedError' error 
    DEF fuqian (Self, Money):   # this is not to use pay function 
        Print ( ' already using Apple paid RMB% s ' % Money) 

DEF pay (pay_obj, Money):
     # unified pay entrance Either way, mainly paid on it 
    pay_obj.pay (Money) 

wechat = Wechatpay ()
 #Examples of 
Ali = Alipay ()
 # instantiated 
App = Aapplepay ()
 # instantiated 
# after the beginning of the use of specifications, you do not need to pay before making the call may be given a method, and can be instantiated directly, 
# so easily write code that people will soon be able to find out where the error 
# pay (wechat, 100) # payment 100 through micro letter 
# pay (Ali, 200) # 200 is payable through Paypal 
# pay (App, 300) # here call pay function, but front Applepay not pay to use the function inside but fuqian, so this error will result

operation result:

Traceback (most recent call last):
  File "<encoding error>", line 32, in <module>
TypeError: Can't instantiate abstract class Aapplepay with abstract methods pay

This can be achieved substantially lead interface class in code structure, similar structure is shown below:

from ABC Import AbstractMethod, ABCMeta
 class Payment (= the metaclass that ABCMeta):   # default metaclass type 
    @abstractmethod
     DEF pay (Self, Money):   # Specification subclass must pay method comprising 
        Pass 
class A (Payment):
     DEF pay (Self, Money): Pass 

class B (Payment):
     DEF Pay (Self, Money): Pass 

class C (Payment):
     DEF Pay (Self, Money): Pass

In fact, classes are abstract classes and interfaces play a subclass specification, a constrained role subclasses, different places are:

Interfaces: Supports multiple inheritance, all methods of the interface class must not be achieved, so as to play a regulatory role in --- Java

Abstract class: does not support multiple inheritance, methods abstract class can implement some code --- Java in

 

Guess you like

Origin www.cnblogs.com/wxm422562/p/11006498.html