Python OOP Detail

data attribute

Python OOP Detail_第1张图片
 

方法
Python OOP Detail_第2张图片
 

调用超类构造函数

Python OOP Detail_第3张图片
 

重写方法
Python OOP Detail_第4张图片
 

interface

#Super 定义一个method函数以及在子类中期待一个动作的delegate
class Super:
    def method(self):
        print('in Super.method')    #Default behavior
    def delegate(self):
        self.action()               #Expected to be defined


#Inheritor 没有提供任何新的变量名,因此会获得Super中定义的一切内容
class Inheritor(Super):             #Inherit method verbatim
    pass


#Replacer 用自己的版本覆盖Super的method
class Replacer(Super):              #Replace method completely
    def method(self):
        print('in Replacer.method')


#Extender 覆盖并返回默认method,从而定制Super的method
class Extender(Super):              #Extend method behavior
    def method(self):
        print('starting Extender.method')
        Super.method(self)
        print('ending Extender.method')


#Provider 实现Super的delegate方法预期的action方法
class Provider(Super):              #Fill in a required method
    def action(self):
        print('in Provider.action')


if __name__ == '__main__':
    for instance in (Inheritor, Replacer, Extender):
        print('\n' + instance.__name__ + '...')
        instance().method()
    print('\nProvider...')
    x = Provider()
    x.delegate()

 测试结果:

Python OOP Detail_第5张图片
 

抽象基类:assert、NotImplementedError

Python OOP Detail_第6张图片
Python OOP Detail_第7张图片
 

@abstractmethod
Python OOP Detail_第8张图片
 

命名空间

Python OOP Detail_第9张图片
 

Summary

抽象类是会调用方法的类,但没有继承或定义该方法,而是期待该方法由子类填补。当行为无法预测,非得等到更为具体的子类编写时才知道,通常可用这种方式把类通用化。OOP软件框架也使用这种方式作为客户端定义、可定制的运算的实现方法。

 

当简单赋值语句(x = y)出现在类语句的顶层时,就会把数据属性附加在这个类上(Class.x)。就像所有的类属性,这会由所有的实例共享。不过,数据属性并不是可调用的方法函数。

 

如果类定义自身的__init__构造函数,但是也必须启动超类的构建其代码,就必须手动调用超类的__init__方法。超类的构造函数式通过类名称来调用,手动传入self实例:Superclass.__init__(self, ...)

 

要增强继承的方法而不是完全替代,还得在子类中进行重新定义,但是要从子类的新版方法中,手动回调超类版本中的这个方法。也就是,把self实例手动传给超类版本的这个方法:Superclass.method(self,...)

 

 

 

 

 

 

你可能感兴趣的:(python)