A composition of the members of the sub-classification of
Before we talked broadly divided into two classes region, static fields and methods section portion.
A detailed breakdown of each area can be divided into:
class A: Company = " Ali Baba ' # static variables (static field) __tel = " XXXXXXX " # private static variable (private static fields) DEF the __init__ (Self, name, Age): # special method (method for initializing) the self.name name = # object attribute (the normal field) Self. __age = Age # private object attributes (common private field) DEF func1 (Self): # common method pass DEF __func (Self): # private methods Print (666 ) @classmethod # class method DEF class_func (cls): "" " define a class method, at least one parameter cls " "" Print ( ' class method ' ) @staticmethod # static method DEF static_func (): "" " define a static method, no default parameters " "" Print ( ' static method ' ) @property # 属性 def prop(self): pass
II. Class private members
For the members of each class has two forms:
Members of the public , can be accessed from anywhere
Private member internal to the method, only the class
Limit public access private members and members of the different
1. Properties
Static properties
Object Properties
Static fields (static properties)
Public static fields: class can access; inner class can access; derived class can access
Private static fields: internal class can only access;
Public static properties (fields)
class C: name = " public static field " def func(self): print C.name class D(C): def show(self): print C.name C.name # class accesses obj = C () obj.func () # internal class can access obj_son = D () obj_son.show () # derived class can access
Private static attribute (field)
class C: __name__ = " private static fields ." def func(self): print C.__name class D(C): def show(self): print C.__name C. __name__ # inaccessible external obj = C () obj. __name__ # inaccessible externally obj.func () # internal class can access obj_son = D () obj_son.show () # can not be accessed in a derived class
Public object properties
class C: def __init__(self): self.foo = " Public field " DEF FUNC (Self): Print self.foo # class internal access class D (C): DEF Show (Self): Print self.foo # derived class access obj = C() obj.foo # accessed through the object obj.func () # class internal access obj_son = D (); obj_son.show () # derived class access
Private object attributes
class C: def __init__(self): Self. __foo = " private field " DEF FUNC (Self): Print self.foo # class internal access class D (C): DEF Show (Self): Print self.foo # derived class access obj = C() . obj __foo # accessed through the object ==> Error obj.func () # class internal access ==> correct obj_son = D (); obj_son.show () # derived class access ==> Error
2. Methods
Public methods: an object can be accessed; internal access classes; derived class can access
Private methods: internal class can only access;
(1) Public Method
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 () # objects are accessed via obj.func () # internal access class obj.add () # derived class access
(2) Private Method
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 () # accessed through the object can not obj.func () # internal access class obj. __add () # derived class can not access
Summary: For these private members, they use only within the class, you can not be outside the class and derived class to use.
Have to access private members, it can be the object ._ class __ attribute name, but definitely not !!! Why can ._ __ private members of the class name to access it? Because when you create a class, if you encounter a private member (including private static field, the normal field private, proprietary method) it will automatically saved in memory when preceded by the name of the class _.
Other members of the III. Class
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 (you may also transmit properties and methods and the like);
Call: 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 to pass the properties and methods (not pass instance attributes and methods) of the class;
Call: class instance object and the object can be called.
Static method
Definition: the 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: class instance object and the object can be called.
The method of the bis (will be mentioned later)
Definition: The Double Down is a special method, he is a cool method name with an underscore plus cool underscore method has special significance __ __ method name interpreter provided under the dual method is mainly python source programmers,
我们在开发中尽量不要使用双下方法,但是深入研究双下方法,更有益于我们阅读源码。
调用:不同的双下方法有不同的触发方式,就好比盗墓时触发的机关一样,不知不觉就触发了双下方法,例如:__init__