More Related Content Similar to Python面向对象开发基础篇 Similar to Python面向对象开发基础篇 (20) Python面向对象开发基础篇4. 抽象
基于分类的思考方法
Window
Dialog Widget
MessageBox Panel Button
具体
5. 定义了功能模板,是一
类事物的集合,是抽象
概念
Class
derive, subclass
inherit
class
6. 类的组成
Window 名称
width
height
background 属性
• public
• private color
• protect font
draw()
fresh()
destroy() 方法
move()
resize()
7. instance = class()
一个类可以创建多个实例,每个实例可
以拥有不同的属性值
class 杯子
玻璃杯 = 杯子(材料=玻璃)
红色金属杯 = 杯子(材料=金属,颜色=红
色)
9. 封装 数据+方法
继承 子类可以使用父类
的属性和方法
覆盖 改变父类的属性值和方法实现
多态 同一个父类可以派
生出不同的子类
13. import os
class Command(object):
def __init__(self, name):
self.name = name
def __call__(self, *args, **kwargs):
raise NotImplementedError
class DirCommand(Command):
def __call__(self, dir):
os.system('dir %s' % dir)
dir_cmd = DirCommand('dir')
dir_cmd('.')
15. • obj = Class(*args, **kwargs) =>
obj = Class().__init__(*args, **kwargs)
• obj.attr 可以访问类中定义的属性
• obj.func()可以调用类中定义的方法
• 属性可以在运行时动态绑定
17. old style class和new style class
>>> class A:pass
>>> type(A)
<type ‘classobj’>
>>> type(int)
<type ‘type’>
>>> class A1(object):pass
>>> type(A1)
<type ‘type’>
18. new style class的作用
• 在Python 2.2之前,类与类型是不统一的
• 在Python 2.2实现了统一
• 让内置的类型子类化更方便
• 提供了属性(property)的机制
• 实现了static和class方法
• 提供了metaclass编程
Unifying types and classes in Python 2.2
PEP-252: Making types look more like classes
PEP-253: Subtyping built-in types
19. 类
class A(object):pass
class B(A):
“””B description”””
def out_b(self, name):
print name
class C(A):
def out_c(self, name):
print name
class D(B, C):pass
>>> d = D()
>>> d.out_b('hello')
hello
20. • 一个类可以有一个或多个基类,多个基类
之间使用’,’分隔
• 类下可以定义doc_string
• 如果不存在基类,则为一个old-style类
• 只有当所有基类都是old-style类时,子类才
是old-style类,否则就是new-style类
• 如果不需要基类,则可以把object作为基类
来创建new-style类
• 在3.x将不存在old-style类,所有的类都是
new-style,所以可以省略object
22. • 在方法中通过 类 变量来使用类属性
• 属性可以动态绑定到类或实例变量上
class C(object):
x = 20
def call(self):
C.x += 2
print C.x
print self.__class__
C.y = 21
24. • dir(D) 可以列出类的属性名,包括方法
• D.__name__ 可以得到类的名称
>>> D.__name__
‘D’
• D.__module__ 得到类所在的模块
• D.__bases__ 得到所有基类
>>> D.__bases__
(<class '__main__.B'>, <class '__main__.C'>)
25. 如何判断一个对象是类
考虑使用types,它定义了许多类型,但是:
>>> import types >>> import types
>>> class A:pass >>> class B(object):pass
>>> isinstance(A, types.ClassType) >>> isinstance(B, types.ClassType)
True False
>>> isinstance(A, types.TypeType) >>> isinstance(B, types.TypeType)
False True
27. 类私有属性
• 以两个下划线开始的属性,python会把它编译
为:_classname__attribute
• 其实python中不存在真正的私有属性
• 更多是采用约定的方式,如以’_’开始的
>>> class C5(object):
... __private = 23
>>> print C5.__private
AttributeError: class A has no attribute ' private'
>>> print C5. _C5__private
23
28. static和class方法
>>> class A(object):
... @staticmethod
... def static():
... print A.__name__
... @classmethod
... def method(cls):
... print cls.__name__
>>> a = A()
>>> a.static()
‘A’
>>> A.static()
‘A’
>>> a.method()
‘A’
>>> A.method()
‘A’
29. • 静态方法:类相当于只是起到一个作用域
限制的作用,可以通过实例,类来访问
• 类方法:第一个参数是类对象,可以通过
实例,类来访问。与实例无关的方法可以
定义为类方法。
• 实例方法:第一个参数是实例对象,只能
通过实例来访问。
31. 创建实例
• 创建一个实例,只要象写调用一 >>> d = D()
个函数一样调用类对象。
>>> isinstance(d, D)
True
• 如果类定义了__init__方法,则创 >>> class E(object):
建实例时会把参数传给它来执 ... def __init__ (self, n):
行。
... self.x = n
>>> e = E(42)
>>> print e.x
42
>>> e.z = 8
• 可以动态向实例绑定新的属性 >>> print e.z
8
34. MRO(Method Resolution Order)
• 先查找实例的属性(self.__dict__)
• 再在实例的类中查找(self.__class__.__dict__)
• 再到基类中查找
• 如果存在多个基类,根据mro的顺序
>>> D.mro()
[<class '__main__.D'>, <class
'__main__.B'>, <class '__main__.C'>, <class
'__main__.A'>, <type 'object'>]
37. 如何调用父类的方法
class A(object):
def __init__(self):
print 'A'
class B(A):
def __init__(self):
super(B, self).__init__()
print 'B'
b = B()