Interface class (abstract):
In the python is no interface classes, abstract classes and interfaces can be considered as the same class
definition: to develop a specification
for example: project manager received a request, write a payment:
# The first version of class Alipay: DEF __init__ (Self, Money): self.money = money DEF Pay (Self): Print ( ' ZFB paid S% ' % self.money) class Jd: def __init__(self, money): self.money = money DEF Pay (Self): Print ( ' JD paid S% ' % self.money)
A1 = Alipay (200 is) a1.pay () A2 of Jd = (100) a2.pay ()
# Second Edition, improved: the method of payment of the same class Alipay: DEF __init__ (Self, Money): self.money = money DEF Pay (Self): Print ( ' ZFB paid S% ' % self.money) class of Jd: DEF the __init__ (Self, Money): self.money = money DEF Pay (Self): Print ( ' JD paid S% ' % self.money) DEF Pay (obj): obj.pay() a1 = Alipay(200) j1 = Sn (100 ) the Pay (a1) # put this method a1 passed pay () function, through its unified look for the function body to access the contents of # normalized design, improve maintainability
Pay than the last defined () function in the class, each generation of the objects within the brackets in the function block,
Pay call content in block () of the present method is to find the class, then look for the parent.
Since the two classes have the same Pay () method, this function can be achieved external unified operation invocation.
# Third edition came a wild programmer, a micro-channel pay increase functionality class Alipay: DEF __init__ (Self, Money): self.money = money DEF Pay (Self): Print ( ' ZFB paid S% ' % self.money) class Jd: def __init__(self, money): self.money = money DEF Pay (Self): Print ( ' JD paid S% ' % self.money) class Wechatpay: def __init__(self, money): self.money = Money DEF weixinpay (Self): Print ( ' WX paid S% ' % self.money) def pay(obj): obj.pay() # # W1 = Wechatpay (300) # w1.weixinpay () # newly defined class who is not identical with the first two classes Pay () method can not achieve all classes normalized operation
Go back and rewrite the fourth edition of hair, to develop a rule (abstract class, interface class)
from abc Import ABCMeta, AbstractMethod
# enforce this interface class
class Payment (metaclass = ABCMeta): # is inherited by all sub-classes, abstract classes, interfaces class
@abstractmethod # decorator
DEF the Pay (Self):
Pass # developed a specification that requires all inherited my subclass must have a pay () method, if any, in the instantiation error
# important method name, content does not require
class Alipay (payment):
DEF the __init __ (Self, Money):
self.money = Money
DEF pay (Self):
Print ( 'ZFB paid S%'% self.money)
class of Jd (payment):
DEF __ the __init (Self, Money):
self.money = Money
DEF pay (Self):
Print ( 'JD paid% s'% self.money)
Wechatpay class (Payment):
DEF the __init __ (Self, Money):
self.money = Money
DEF Pay (Self):
Print ( 'WX paid S%'% self.money)
DEF Pay (obj):
obj.pay ()
w1 = Wechatpay (300) # At this time, if this is not a class Pay () method is an instance of a failure, an error will be
# think you must have