python tutorial 学习笔记(六)class

class ClassName( SuperClass ) :

A new name space is created, thus all assignments to local variables go into this new namespace.

When a class definitition ends, a class object is created.

attribute names: __doc__( the fist multil-line string ), __class__(class object), variable, function object

instantiation: __init__(self). How to create an instance: X = MyClass()

Instance Objects:

Attribute references: data attribute and method. MyClass.f is a function object, x.f is a method object.

Data attibutes override method attributes with the same name. Note that clients may add data attributes of their own to an instance object

Method Objects:

They are attribute of Instance Objects. the special thing about methods is that the object is passed as the first argument of the function

x.f()  is equivalent to MyClass.f(x)

Inheritance:

Method reference are resolved as : the corresponding class attribute, descending down the chain of base classes.

All mehods in Python are virtual.

class BaseClass:
    def toString(self):
        return "in BaseClass"

    def f(self):
        print self.toString()
class MyClass(BaseClass):
    def toString(self):
        return "in MyClass"

    def fb(self):
        BaseClass.f(self)

if __name__ == "__main__":
    x = MyClass()
    x.fb()

>>in MyClass

Two builtin functions: isinstance(obj,class), issubclass(class1,class2)

Multiple Inheritance:

class DerivedClassName(Base,Base2,Base3):

old-style classes’s method reference rule: depth-first, left-right

new-style class( any class which inherite from “object”):http://www.python.org/download/releases/2.3/mro/ .(too complicated for now, leave it for later)

Private Variables:

__spam is replaced with _classname__spam, but “__spam__” won’t.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值