Members of the class: the 6.8 Object-oriented
Composed of members of the subdivisions
Class roughly divided into two regions, as follows
class A: name="haha"#第一部分 静态字段(静态变量) def __init__(self):#第二部分 方法部分 pass def func(self):#第二部分:方法部分 pass
Specific can be broken down as follows:
class A: company_name="haha" #静态变量 (静态字段) _iphone="564545"#私有静态变量(私有静态字段) def __init__(self,name,age):#特殊方法 self.name=name#对象属性 self._age=age#私有对象属性(私有普通字段) def func1(self):#普通方法 pass def __func(self):#私有方法 print(666) @classmethod#类方法 def class_func(cls): """定义类方法,至少有一个cls参数""" print('类方法') @staticmethod#静态方法 def static_func(): """定义静态方法,无默认参数""" print("静态方法") @property#属性 def prop(self): pass
Private members of the class
There are two forms for each member of the class: public members: can be accessed anywhere; private members only internal access to the class
Restrict access private members and members of the public differ:
Static fields (static properties)
Public static fields: class can access; inner class can access; derived class can access
class C: name = "公有静态字段" def func(self): print C.name class D(C): def show(self): print C.name C.name # 类访问 obj = C() obj.func() # 类内部可以访问 obj_son = D() obj_son.show() # 派生类中可以访问
Private static fields: internal-only access to
class C: __name = "私有静态字段" def func(self): print C.__name class D(C): def show(self): print C.__name C.__name # 不可在外部访问 obj = C() obj.__name # 不可在外部访问 obj.func() # 类内部可以访问 obj_son = D() obj_son.show() #不可在派生类中可以访问
The normal field (object attribute)
Common Public field: an object can be accessed; internal access classes; derived class can access
class C: def __init__(self): self.foo = "公有字段" def func(self): print self.foo # 类内部访问 class D(C): def show(self): print self.foo # 派生类中访问 obj = C() obj.foo # 通过对象访问 obj.func() # 类内部访问 obj_son = D(); obj_son.show() # 派生类中访问
Private general fields: internal class can only access
class C: def __init__(self): self.__foo = "私有字段" def func(self): print self.foo # 类内部访问 class D(C): def show(self): print self.foo # 派生类中访问 obj = C() obj.__foo # 通过对象访问 ==> 错误 obj.func() # 类内部访问 ==> 正确 obj_son = D(); obj_son.show() # 派生类中访问 ==> 错误
method:
Public methods: an object can be accessed; internal access classes; derived classes can access
class C: def __init__(self): pass def add(self): print('in C') class D(C): def show(self): print('in D') def func(self): self.show() obj = D() obj.show() # 通过对象访问 obj.func() # 类内部访问 obj.add() # 派生类中访问
Private methods: internal class can only access
def __init__(self): pass def __add(self): print('in C') class D(C): def __show(self): print('in D') def func(self): self.__show() obj = D() obj.__show() # 通过不能对象访问 obj.func() # 类内部可以访问 obj.__add() # 派生类中不能访问
Summary: For these private members, they use only within the class, you can not be outside the class and derived class to use.
* Ps: have to access private members, it can be the object ._ class __ attribute name, but definitely not !!! *
Other members of the class: the other members of this class is the main method: Methods include: general methods, static methods and class methods, three methods all belong to the class in memory , the difference lies in the different ways to call.
- Examples of methods:
- Definition: The first parameter is an instance of an object, the parameter name is generally agreed as: "self", through which pass properties and methods of instances (may also transmit properties and methods and the like);
- Call: it can only be called by an instance of an object
- Class Methods:
- Definition: the decorator @classmethod. The first argument must be a class object is the current, this parameter is generally agreed to name "cls", through which the wear properties and methods (Examples can not pass the attributes and methods) primer class;
- Call: the instance object and the object class can call
- Static methods:
- Definition: decorator @staticmethod random parameters, no "self" and "cls" parameter, but the method can not be used in the body of any class or instance attributes and methods;.
- Call: the instance object and the object class can call
- Examples of methods: