python 面向对象编程

1.引言

新式类:

class MyNewObjectType(bases):
    'define MyNewObjectType class'
    class_suite

经典类:没有指定一个父类,或者子类化的基本类没有父类,就创建了一个经典类

class MyNewObjectType:
    'define MyNewObjectType classic class'
    class_suite

创建一个实例,只使用类作为名称空间容器

class MyData(object):
    pass

mathObj = MyData()
mathObj.x = 4
mathObj.y = 5
print mathObj.x + mathObj.y
print mathObj.x * mathObj.y
mathObj.x和mathObj.y是实例属性,因为它们不是类MyData的属性,而是实例对象(mathObj)的独有属性。
class MyDataWithMethod(object):   #定义类
    def printFoo(self):    #定义方法,不需要自己传入self,因为它是自动传入的
        print 'You invoked printFoo()!'

myObj = MyDataWithMethod()  #创建实例
myObj.printFoo()      #调用方法
# 创建一个类(类定义)
class AddrBookEntry(object):
    'address book entry class'
    def __init__(self,nm,ph):
        self.name = nm
        self.phone = ph
        print 'created instance for: ', self.name

    def updatePhone(self, newph):
        self.phone = newph
        print 'updated phone# for:', self.name

# 创建实例(实例化) 会自动调用__init__()
john = AddrBookEntry('John Doe', '408-555-1212')  
jane = AddrBookEntry('Jane Doe', '650-55-1212')

# 方法调用(通过实例)
john.updatePhone('415-555-1212')  #更新John Doe的电话
print john.phone

# 创建子类
class EmplAddrBookEntry(AddrBookEntry):
    'Employee Address Book Entry class'  #员工地址薄类
    def __init__(self, nm, ph, id, em):
        AddrBookEntry.__init__(self, nm, ph)
        self.empid = id
        self.email = em

    def updateEmail(self, newem):
        self.email = newem
        print 'Updated e-mail address for:', self.name

# 使用子类
john = EmplAddrBookEntry('John Doe', '408-555-1212',42, 'john@spam.doe')

每个子类最好定义它自己的构造器,不然,基类的构造器会被调用。如果子类重写基类的构造器,基类的构造器就不会自动调用了–这样,基类的构造器就必须显式写出才会被执行。

1.类的属性

查看类的属性:dir()或者通过访问类的字典属性__dict__
特殊的类属性

特殊类属性
C.__name__类C的名字(字符串)
C.__doc__类C的文档字符串
C.__bases__类C的所有父类构成的元组
C.__dict__类C的属性
C.__module__类C定义所在的模块
C.__class__实例C对应的类(仅新式类中)

2.继承

一个子类可以继承它的基类的任何属性,不管是数据属性还是方法。

class P(object):
    pass

class C(P):
    pass

c=C()        #实例化子类
c.__class__  #<class '__main__.C'>
C.__bases__  #<class '__main__.P'>
1.通过继承覆盖方法
class P(object):
    def foo(self):
        print 'Hi, I am P-foo()'

class C(P):
    def foo(self):
        print 'Hi, I am C-foo()'

#在子类中调用基类方法
class C(P):
    def foo(self):
        super(C, self).foo()
        print 'Hi, I am C-foo()'
2.多重继承
# 经典类简单属性查找示例(深度优先)
class P1:
    def foo(self):
        print 'called P1-foo()'

calss P2:
    def foo(self):
        print 'called P2-foo()'
    def bar(self):
        print 'called P2-bar()'

class C1(P1, P2):  # 子类1, 从P1,P2派生
    pass

calss C2(P1, P2):
    def bar(self):
        print 'called C2-bar()'

class GC(C1, C2):   #定义子孙类,从C1 C2派生
    pass

gc = GC()
gc.foo()  #P1-foo()  GC => C1 => P1
gc.bar()  #P2-bar()  GC => C1 => P1 => P2
C2.bar(gc)  #调用C2的bar()方法

# 新式类  广度优先搜索
class P1(object):
    def foo(self):
        print 'called P1-foo()'

calss P2(object):
    def foo(self):
        print 'called P2-foo()'
    def bar(self):
        print 'called P2-bar()'

class C1(P1, P2):  # 子类1, 从P1,P2派生
    pass

calss C2(P1, P2):
    def bar(self):
        print 'called C2-bar()'

class GC(C1, C2):   #定义子孙类,从C1 C2派生
    pass

gc = GC()
gc.foo()  # P1-foo()  GC => C1 => C2 =>P1
gc.bar()  # C2-bar()  GC => C1 => C2
P2.bar(gc)   #调用P2的方法

3.*attr()系列函数

class MyClass(object):
    def __init__(self):
        self.foo = 100

my_inst = MyClass()
hasattr(my_inst,'foo') #True
getattr(my_inst,'foo') #100
hasattr(my_inst,'bar') #False
getattr(c,'bar','oops') # 'oops'
setattr(my_inst, 'bar', 'my attr')
dir(my_inst)  #['__doc__','__module','bar', 'foo']
getattr(my_inst, 'bar')   #等同于my_inst.bar  'my attr'
delattr(my_inst, 'foo')
dir(my_inst)   #['__doc__', '__module__', 'bar']

super()

不懂,主要用途是用来查找父类的属性

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值