Create Class
The Python class is created using the class keyword. A simple class declaration can be a keyword followed by a class name:
class ClassName(bases): ‘class documentation string‘#‘类文档字符串‘ #类体
Instantiation of
Instantiate a class with a class name followed by a pair of parentheses
MC = MyClass () # Instantiate class initialization classes
‘
int() ' Constructor
def __int__(self): pass
Note: Self is similar to the Java "This" keyword, which code points to a reference to its own instance
Class properties
Unlike object-oriented languages such as Java and C + +, Python's properties include data members and function elements that are accessed through a period symbol.
Special data built-in properties
C. Name of Class C (string)
C. Document string fordoc Class C
C. Tuples of all parent classes ofbases Class C
C. Properties of theDict Class C
C. Module Class C definition (new in version 1.5)
C. classes corresponding to class instance C (in the new class only)
Special method built-in properties
Dir (): Gets the class attribute or instance property name list.
Static variable Properties
Defined directly in the class scope
class C(object): 100
Instance variable properties
The instance properties of Python are different from Java and C + +. In Java and C + +, instance properties must first be declared/defined, while Python instance properties are created dynamically. Setting the properties of an instance can take place at any time after the instance is created, or in code that can access the instance. Structure
is one of the key points for setting these properties.
def __init__(self, name, data): self.name = name "123‘
Note: Self is similar to the Java "This" keyword, which code points to a reference to its own instance
Method Properties
It is divided into instance methods and class methods. The instance method belongs to only one instance, and the class method belongs to the class, and it belongs to the instance.
Instance method
class MyClass(object): def myNoActionMethod(self): pass
Note: Self is similar to the Java "This" keyword, which code points to a reference to its own instance
Static methods
A static method is a class-level method that can be called directly without instantiating a class. There are two ways to define
@staticmethod def foo(): print‘call static method‘
def foo(): print‘call static method‘ #静态方法
Class method
A static method is a class-level method, and unlike a static method, it must display the passed-in CLS class parameter, and if you need to call other static methods in the class, or a function of a class method, define a class method. Similar to static methods, there are two methods of defining them.
@classmethod def bar(cls): print‘call class method and access static varible(staticVar): ‘, cls.staticVar
def bar(cls): print‘call class method and access static varible(staticVar): ‘, cls.staticVar bar = classmethod(bar) #类方法
Detailed examples
#!/usr/bin/python#coding =utf-8 class Target(): #定义类Target ' This is Target definition ' #定义__doc__属性Staticvar =' v1.0 ' #定义静态变量 def __init__(self, name = ' Default ', data = 0): #定义构造函数Self.name = Name#实例变量Self.data = Data#实例变量 Print "init instance" def main(): Print "This is a test function" " can define static methods with decorators @staticmethod def foo (): print ' call static method ' def foo(): Print ' call static method 'foo = Staticmethod (foo)#静态方法 " You can define class methods with Adorners @classmethod def Bar (CLS): print ' Call class method and Access static varible (stat Icvar): ', Cls.staticvar ' def bar(CLS): Print ' Call class method and Access static varible (Staticvar): ', Cls.staticvar bar = Classmethod (bar)#类方法 #只有调用本模块的时候main () method only takes effect if__name__ = =' __main__ ': Main ()#实例化target = Target (' AAA ',123)Print ' name is: ', Target.namePrint ' data is: ', Target.data#打印__doc__属性Print ' target.__doc__ is: ', target.__doc__#打印类__dict__属性Print ' target.__dict__ is: ', target.__dict__#打印静态变量Print ' Staticvar is: ', Target.staticvar#打印内建函数dir ()Print ' Dir () is: ', dir (Target)#调用静态方法Target.foo ()#调用类方法Target.bar ()
Output
This isA Test functioninit InstanceName is: Aaadata is:123target.__doc__ is: this isTarget definitiontarget.__dict__ is: {' __module__ ':' __main__ ',' foo ': <staticmethodObjectAt0X7F3FD9310CC8,' Bar ': <classmethodObjectAt0x7f3fd9310d38,' Staticvar ':' v1.0 ',' main ': <function Main at0x7f3fd930e758,' __doc__ ':' This is Target definition ',' __init__ ': <function __init__ at0x7f3fd930e6e0>}staticvar is: v1. 0Dir () is: [' __doc__ ',' __init__ ',' __module__ ',' Bar ',' foo ',' main ',' Staticvar ']callStaticMethodcall class method and AccessStaticVarible (Staticvar): v1. 0
Python Object-oriented explanation (top)