Python(13)面向对象

Python(13)面向对象

  • 面向对象
  • 实例
  • 绑定/静态/类方法
  • 组合、子类、派生、继承
  • 类、实例和对象的内建函数

面向对象

面向对象实现了数据与动作的融合

Python面向对象的常用术语

抽象/实现
合成
派生/继承/继承结构
泛化/特化
多态
自省/反射

类是一种数据结构,可以用类定义对象,对象把数据值和行为特性融合在一起。
类是一种模型,用来产生真实的物体(实例)
类是对一个对象的定义,实例是真正的实物
创建一个类:

class MyClass(object):
    def func():
        pass

类属性

属性是属于一个对象的数据或者函数元素,可以通过句点标识来访问对象的属性

第一种:数据属性

class Foo(object):
    fo = 100 # 静态数据 或者 静态变量
print Foo.fo

Foo.fo = Foo.fo + 1
print Foo.fo

100
101

第二种:方法属性
方法只有被绑定到一个实例才能直接被调用。

class Foo(object):
    def func(self): # 必须要有self。self代表实例对象本身,当用实例调用方法时,解释器会传递给方法。
        print 'hahha'

t = Foo()
t.func()

hahha

第一种确定类属性方法:dir()

class Foo(object):
    def func(self):
        print 'hahha'

print dir(Foo)

['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'func']

第二种确定类属性方法:访问类的字典属性._ _ dict _ _

class Foo(object):
    def func(self):
        print 'hahha'

print Foo.__dict__

{'__dict__': <attribute '__dict__' of 'Foo' objects>, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__doc__': None, 'func': <function func at 0x00000000027C3A58>}

特殊的类属性:

class Foo(object):
    def func(self):
        print 'hahha'

print Foo.__name__   # 类的名字
print Foo.__doc__    # 类的文档字符串
print Foo.__bases__  # 所有父类构成的元组
print Foo.__dict__   # 类的所有属性
print Foo.__module__ # 类的定义所在的模块
print Foo.__class__  # 实例对应的类

Foo
None
(<type 'object'>,)
{'__dict__': <attribute '__dict__' of 'Foo' objects>, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__doc__': None, 'func': <function func at 0x0000000002FD3AC8>}
__main__
<type 'type'>

实例(对象)

实例是运行期的对象

初始化:通过调用类对象创建实例

class Foo(object):
    def func(self):
        print 'hahha'

mc = Foo() # 实例化的实现

init”构造器“方法

把创建实例的调用当成对构造器的调用

class Foo(object):
    def __init__(self):
        print 'hahha'

mc = Foo()

hahha

new”构造器“方法

和init类似

del”解构器“方法

Python中的解构器是在实例释放前提供特殊处理功能的方法。
Python具有垃圾对象回收机制,该函数要直到该实例对象所有的引用都被清除后才会执行。

实例属性

使用缺省参数进行实例化

class Foo(object):

    def __init__(self ,a ,b=10):
        self.a = a
        self.b = b

    def func(self):
        print self.a
        print self.b
fo = Foo(1)
fo.func()

fu = Foo(2,100)
fu.func()

1
10
2
100

绑定/静态/类方法,组合,子类和派生

绑定

方法只有在其所属的类拥有实例时,才能被调用。当存在一个实例时,方法才能被认为是绑定到那个实例。
应为方法的实例在任何方法调用中总是作为第一个参数传递的,self被选中用来代表实例。

调用绑定的方法

class Foo(object):

    def func(self):
        print 'bingding method'

fo = Foo()
fo.func()

bingding method

调用非绑定的方法
该场景并不常用,需要调用一个没有任何实例的类中方法场景:派生一个子类,而且需要覆盖父类的方法。

class Foo(object):
    def func(self):
        print 'bingding method'

class too(Foo):
    def loo(self):
        Foo().func()
        print 'hahah'

fo = too()
fo.loo()

bingding method
hahah

静态方法和类方法

class Foo(object):

    @staticmethod
    def func(self):
        print 'static method'

    @classmethod
    def fun(self):
        print 'class method'

组合

让不同的类混合并加入到其他类中,来增加功能和代码重用性。

class A(object):
    def __init__(self, nm, ph):
        self.name = Name(nm)    # 为name创建实例
        self.phone = Phone(ph)  # 为Phone创建实例
        print 'cfsd'

子类和派生

派生和继承是同一问题的不同描述,父类派生了子类,子类继承了父类;

class A(object):
    def af(self):
        print 'parents'

class B(A):
    def bf(self):
        print 'children'

h = B()
h.bf()
h.af() 

children
parents

继承

base类属性

class A(object):pass
class B(A):pass
class C(B):pass

print A.__base__
print B.__base__
print C.__base__

<type 'object'>
<class '__main__.A'>
<class '__main__.B'>

通过继承覆盖方法

class A(object):
    def foo(self):
        print 'parent foo'
class B(A):
    def foo(self): # 覆盖父类的方法
        print 'child foo'

b = B()
b.foo()

A.foo(b) # 调用父类的方法,因为子类已经实例化了b,所以可以使用该非绑定的方式进行调用

child foo
parent foo

另外、在子类里调用重写的方法:

class A(object):
    def foo(self):
        print 'parent foo'
class B(A):
    def foo(self):
        A.foo(self) # 子类中调用重写父类的方法
        print 'child foo'

b = B()
b.foo()

parent foo
child foo

另外一种方式:

class A(object):
    def foo(self):
        print 'parent foo'
class B(A):
    def foo(self):
        super(B,self).foo() # super不但能找到基类方法,还能为我们传进self
        print 'child foo'

b = B()
b.foo()

parent foo
child foo

从标准类型派生

第一种:不可变类型

第二种:可变类型

多重继承

class P1(object):
    def foo(self):
        print 'parent P1'
class P2(object):
    def too(self):
        print 'parent P2'
class C1(P1,P2):
    def soo(self):
        print 'child C1'
class C2(P1,P2):
    def hoo(self):
        print 'child C2'
class C(C1,C2):
    def loo(self):
        print 'child C'
c = C()
c.loo()
c.hoo()
c.soo()
c.too()
c.foo()

child C
child C2
child C1
parent P2
parent P1

类、实例和对象的内建函数

issubclass()

判断一个类是另外一个的子类或孙类

print issubclass(C1,P1) # C1是P1的子类
print issubclass(C,P1)  # C是P1的孙类

True
True

isinstance()

判断一个对象是另一个类的实例

p = P1()
print isinstance(p,P1)

True

hasattr()

一个对象是否具有某种属性

p = P1()
print hasattr(p,'foo')

True

getattr()

得到一个对象的一个属性

p = P1()
print getattr(p,'foo')

<bound method P1.foo of <__main__.P1 object at 0x00000000029C6860>>

setattr()

对一个实例赋予一个属性

delattr()

删除一个实例属性

dir()

获取一个类的所有方法

super()

在子类中调用父类的相关属性

class A(object):
    def foo(self):
        print 'parent foo'
class B(A):
    def foo(self):
        super(B,self).foo() # super不但能找到基类方法,还能为我们传进self
        print 'child foo'

b = B()
b.foo()

parent foo
child foo

vars()

获取一个对象的参数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值