1. Class constraints
First edition:
class WechatPay:
def pay(self):
print("Payment by cable")
class AliPay:
def pay(self):
print("Payment by cable")
class QQpay:
def fuqian(self):
print("QQ payment")
wei = WechatPay()
ali = AliPay()
qq = QQpay()
wei.pay()
ali.pay()
qq.fuqian()
# When Unified Interface
def pay(object):
object().pay() # QQ payment cannot be normalized
The Second Edition:
class PayClass:
def pay(self):
pass
class WechatPay(PayClass):
def pay(self):
print("Payment by cable")
class AliPay(PayClass):
def pay(self):
print("Payment by cable")
class QQpay(PayClass):
def fuqian(self):
print("QQ payment")
def pay(object):
object().pay()
pay(WechatPay)
pay(QQpay) # QQpay executes the parent pays method but cannot complete the payment
(1) There are two kinds of constraints on classes:
<1> extract the class. Then define the method in the class. In this method, nothing is wrong. Just throw an exception. So all the classes must rewrite this method. Otherwise, they will report wrong when they visit.
<2> Let the metaclass describe the class. Give an abstract method in the metaclass. In this way, the class has to give a concrete implementation of the abstract method. It can also act as a constraint.
The Third Edition:
# Way 1: (Recommended and commonly used)
# raise actively throws exceptions (active error reporting)
class PayClass:
def pay(self):
raise Exception("You have to write a subclass pay Method")
class WechatPay(PayClass):
def pay(self):
print("Payment by cable")
class AliPay(PayClass):
def pay(self):
print("Payment by cable")
class QQpay(PayClass):
def fuqian(self):
print("QQ payment")
def pay(object):
object().pay()
pay(WechatPay)
pay(QQpay) # If there is no pay method in the QQpay class, raise will actively throw an exception (active error reporting)
# Method two
# Abstract class, interface class: make some rules
from abc import ABCMeta,abstractmethod # Abstract class, interface class
class PayClass(metaclass=ABCMeta): # Meta class
@abstractmethod
def pay(self):
raise Exception("You have to write a subclass pay Method")
class WechatPay(PayClass):
def pay(self):
print("Payment by cable")
class AliPay(PayClass):
def pay(self):
print("Payment by cable")
class QQpay(PayClass):
def fuqian(self):
print("QQ payment")
def pay(object):
object().pay()
pay(WechatPay)
pay(AliPay)
pay(QQpay) # If there is no pay method in the QQpay class, it will not conform to the specified rule and cause an error.
'''
**summary: constraint. In fact, it is the constraints of class to class.. Classes must be written xxx Method of manipulation. stay python There are two kinds of neutral constrained formula and method.:
Make abstract classes and abstract methods, Because the source of the case is java and c#So the frequency is still very low.
Make gill an abnormal case. And what you try to throw out is NotImplementError. This is more professional., And the error is clear..(Recommend)
'''
2. super analysis:
super is executed strictly in the order of inheritance of classes
class A:
def f1(self):
print('in A')
class Foo(A):
def f1(self):
super().f1()
print('in Foo')
class Bar(A):
def f1(self):
print('in Bar')
class Info(Foo,Bar):
def f1(self):
# The class name in super is the next class specified to find the class name in mro, and self is the MRO order used in the specified search
super(Info,self).f1() # Foo() Memory address of object # Super (subclass name, subclass mro list)
print('in Info f1')
aa = Info() # Memory address of object
aa.f1()
# Info [Info', Foo', Bar', A', 'object']
a = Foo()
b = a
print(a)
print(b)
print(Info.mro())
obj = Info()
obj.f1()