Constraint super three characteristics of object-oriented class type of depth profiling duck

1. Three characteristics

Package

Many encapsulating the data object into ⼀ in. The function code is fixed to the package ⼀ code blocks, functions, objects, packaged into modules which belong thought encapsulated particular situation specific analysis such as you write ⼀ B is a very ⽜ function. this may also be referred to as that packaged in ⾯ to a subject thought. ⽆ is seemingly the more crucial ⼀ combination of content from the system to ⼀ ⼀ storing anything and Use. this is the package .

inherit

Submenu class can automatically have ⽗ like all other content in addition to private property. White said, and submenus can easily reach of children to Use East ⻄ father. But my friends, ⼀ given to a clear understanding of ⼀ a thing. You must have a father , after the reach of children submenus. order can not be chaotic, implementation inheritance in python is very simple. in a statement class, ⾯ add one hour for parentheses after the class name, you can complete inheritance. so what can be accessed using inherit it? when a simple two classes have the same function or feature from the point of view of code ⾯ layer may be recorded using the inherited form. ⽗ extraction ⼀ a class, the class ⽗ prepared by the same portions of the two classes. then two class inherit this class were taken on it. the benefits of this is written, we can avoid a lot of duplication of functions and write the code. If you go from a semantic analysis of the case. would be much simpler if the context is ⼀ species emerged x y. in this case, y is ⼀ kind of generalization of the concept. x than y more specific. it is then x y kind of submenus, such as cat is ⼀ animal cat inherited animal animals active. cats are also active. At this cat in the creation of the animals have a "dynamic" This property. As another example, the white fine bone ⼀ is a monster. Monsters day ⽣ there ⼀ relatively poor function called "Face to eat," White fine bone ⼀ out ⽣ know how to "eat the Face." At this point the white bone fine inheritance monster.

Polymorphism

With ⼀ objects, a variety of forms. In fact, this in python is not easy to explain white, because we ⼀ Use straight in. Just do not specifically. For example, we create ⼀ variables a = 10, we know this when a is an integer type, but we can make the program a = "alex", this time, a separate warranty into a string type. this is what we all know. but I'll tell you that. this is polymorphic resistance. ⼀ same variable may be a variety of forms

2. Type Ducks

python in a proverb that good, you look like a duck, then you are a duck.

class A:
    def f1(self):
        print('in A f1')
    
    def f2(self):
        print('in A f2')


class B:
    def f1(self):
        print('in A f1')
    
    def f2(self):
        print('in A f2')
        
obj = A()
obj.f1()
obj.f2()

obj2 = B()
obj2.f1()
obj2.f2()

A and B two classes there is no coupling, but in a sense they are a unified standard.

The same function set with the same name, which facilitate the development of these two methods can become mutually duck typing.

Examples abound: str tuple list has index method, which is to unify the norm.

str bytes, etc. This is called cross-duck type.

3. Constraints class

There are two classes of constraints:

The first: an exception is thrown

Extraction ⽗ class and then defined the Remedies in ⽗ class. In this Remedies in nothing with a dry. ⼀ to throw exceptions on it. All submenus that such classes must override the Remedies otherwise. time of the visit will give an error.

class Payment:
    def pay(self,money):
        raise Exception("子类设置pay方法")


class QQpay(Payment):
    def pay(self,money):
        print(f"QQ支付{money}金额")

class Alipay(Payment):
    def pay(self,money):
        print(f"支付宝支付{money}金额")

class Wechatpay(Payment):
    def zhifu(self,money):
        print(f"微信支付{money}金额")


def pay(obj,money):
    obj.pay(money)


obj1 = QQpay()
pay(obj1,100)

obj2 = Alipay()
pay(obj2,200)

obj3 = Wechatpay()
pay(obj3,300)

"""
QQ支付100金额
Traceback (most recent call last):
支付宝支付200金额
File "D:/pycharm demo/练习/代码练习.py", line 30, in <module>
  pay(obj3,300)
File "D:/pycharm demo/练习/代码练习.py", line 20, in pay
  obj.pay(money)
File "D:/pycharm demo/练习/代码练习.py", line 3, in pay
  raise Exception("子类设置pay方法")
Exception: 子类设置pay方法
"""

The second: metaclasses

Use metaclass described ⽗ class are given in ⼀ abstract Remedies metaclass. Such classes would have submenus give specific implementation of the abstract methods for the camera. Constraints may also play effect.

from abc import ABCMeta,abstractmethod

class Payment(metaclass=ABCMeta):
    @abstractmethod
    def pay(self,money):
        pass


class QQpay(Payment):
    def pay(self,money):
        print(f"QQ支付{money}金额")

class Alipay(Payment):
    def pay(self,money):
        print(f"支付宝支付{money}金额")

class Wechatpay(Payment):
    def zhifu(self,money):
        print(f"微信支付{money}金额")


def pay(obj,money):
    obj.pay(money)


obj1 = QQpay()
pay(obj1,100)

obj2 = Alipay()
pay(obj2,200)

obj3 = Wechatpay()
pay(obj3,300)

"""
QQ支付100金额
Traceback (most recent call last):
支付宝支付200金额
File "D:/pycharm demo/练习/代码练习.py", line 63, in <module>
  obj3 = Wechatpay()
TypeError: Can't instantiate abstract class Wechatpay with abstract methods pay
"""

4.super depth analysis

class A:
    def f1(self):
        print('in A f1')
    
    def f2(self):
        print('in A f2')


class Foo(A):
    def f1(self):
        super().f2()
        print('in A Foo')
        
        
obj = Foo()
obj.f1()

"""
'in A f2'
'in A Foo'
"""
super可以下一个类的其他方法
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):
        super().f1()
        print('in Info f1')

obj = Info()
obj.f1()

'''
in Bar
in Foo
in Info f1
'''
print(Info.mro())  # [<class '__main__.Info'>, <class '__main__.Foo'>, <class '__main__.Bar'>, <class '__main__.A'>, <class 'object'>]

super()严格按照类的mro顺序执行
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):
        super(Foo,self).f1()
        print('in Info f1')

obj = Info()
obj.f1()

"""
in Bar
in Info f1
"""

Guess you like

Origin www.cnblogs.com/beichen123/p/11316992.html