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‘ #‘类文档字符串‘
class_suite #类体
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):
foo = 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. Constructing the
init () is one of the key points for setting these properties.
-
Span class= "PLN" > def __init__ ( self name data
-
Span class= "PLN" > self name = name
-
self data = "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
Class method
The
static method is a class-level method, and unlike a static method, it must display the passed-in CLS class parameters, and if you need to invoke other static methods in the class, or the functions of a class method, you define a class method. Similar to static methods, there are two methods of defining them.
Decorators (Common)
@classmethod
def bar(cls):
print ‘call class method and access static varible(staticVar): ‘, cls.staticVar
Built-in functions
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"
‘‘‘
可以用装饰器定义静态方法
@staticmethod
def foo():
print ‘call static method‘
‘‘‘
def foo():
print ‘call static method‘
foo = staticmethod(foo) #静态方法
‘‘‘
可以用装饰器定义类方法
@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) #类方法
#只有调用本模块的时候main()方法才生效
if __name__ == ‘__main__‘:
main()
#实例化
target = Target(‘aaa‘, 123)
print ‘name is: ‘, target.name
print ‘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()
From for notes (Wiz)
Python Object-oriented (top)