---
title: "Python OOP Detail"
author: "fairy_xzc"
date: "2013-04-24T13:00:39Z"
tags: []
categories: ["编程语言"]
slug: "python-oop-detail"
draft: false
lastmod: "2013-04-24T13:03:10Z"
id: 1852778
url: "https://agilestyle.iteye.com/blog/1852778"
---

**data attribute**

![](http://dl.iteye.com/upload/attachment/0083/5861/89b9ba78-9747-3133-a0d1-bf1ac67481e2.png)  
 

**方法**  
![](http://dl.iteye.com/upload/attachment/0083/5863/828be9aa-98c2-3a35-be36-244f8a573df4.png)  
 

**调用超类构造函数**

![](http://dl.iteye.com/upload/attachment/0083/5865/2e842056-2099-36a5-9d46-a6fd046828bb.png)  
 

**重写方法**  
![](http://dl.iteye.com/upload/attachment/0083/5867/1f6604fd-b4c8-3ade-b7fb-2d041794874d.png)  
 

**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()
```

 测试结果：

![](http://dl.iteye.com/upload/attachment/0083/5869/06372827-b8b6-3cbc-9ee1-4bbe16886677.png)  
 

**抽象基类：assert、NotImplementedError**

![](http://dl.iteye.com/upload/attachment/0083/5871/51660834-e9a3-36f6-931f-050cc99ffc50.png)  
![](http://dl.iteye.com/upload/attachment/0083/5875/462ddc62-5ad5-3d7c-95eb-824616781855.png)  
 

**@abstractmethod**  
![](http://dl.iteye.com/upload/attachment/0083/5873/f0a0ff0a-6b6a-33f4-9889-4c60407d50de.png)  
 

**命名空间**

![](http://dl.iteye.com/upload/attachment/0083/5877/cbe330b7-74b0-34f7-8746-a4066785f0e7.png)  
 

## Summary

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

 

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

 

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

 

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