python tutorial 学习笔记(六)class

来源:互联网 发布:wc3html闯关软件答案 编辑:程序博客网 时间:2024/05/17 00:12

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.

原创粉丝点击