Python Object-oriented (top)

Source: Internet
Author: User

Create Class

The Python class is created using the class keyword. A simple class declaration can be a keyword followed by a class name:

 
   
  
  1. class ClassName(bases):
  2. ‘class documentation string‘ #‘类文档字符串‘
  3. 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
 
   
  
  1. def __int__(self):
  2. 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

 
   
  
  1. class C(object):
  2. 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.

  
 
    1. Span class= "PLN" > def __init__ ( self name data
    2. Span class= "PLN" > self name = name
    3. 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
 
   
  
  1. class MyClass(object):
  2. def myNoActionMethod(self):
  3. 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

  • Decorators (Common)

      
         
       
    1. @staticmethod
    2. def foo():
    3. print ‘call static method‘
  • Built-in functions

      
         
       
    1. def foo():
    2. print ‘call static method‘
    3. foo = staticmethod(foo) #静态方法
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)

      
         
       
    1. @classmethod
    2. def bar(cls):
    3. print ‘call class method and access static varible(staticVar): ‘, cls.staticVar
  • Built-in functions

        
       
      1. def bar(cls):
      2. print ‘call class method and access static varible(staticVar): ‘, cls.staticVar
      3. bar = classmethod(bar) #类方法
Detailed examples
  
 
  1. #!/usr/bin/python
  2. #coding=utf-8
  3. class Target(): #定义类Target
  4. ‘This is Target definition‘ #定义__doc__属性
  5. staticVar = ‘v1.0‘ #定义静态变量
  6. def __init__(self, name = ‘default‘, data = 0): #定义构造函数
  7. self.name = name #实例变量
  8. self.data = data #实例变量
  9. print "init instance"
  10. def main():
  11. print "this is a test function"
  12. ‘‘‘
  13. 可以用装饰器定义静态方法
  14. @staticmethod
  15. def foo():
  16. print ‘call static method‘
  17. ‘‘‘
  18. def foo():
  19. print ‘call static method‘
  20. foo = staticmethod(foo) #静态方法
  21. ‘‘‘
  22. 可以用装饰器定义类方法
  23. @classmethod
  24. def bar(cls):
  25. print ‘call class method and access static varible(staticVar): ‘, cls.staticVar
  26. ‘‘‘
  27. def bar(cls):
  28. print ‘call class method and access static varible(staticVar): ‘, cls.staticVar
  29. bar = classmethod(bar) #类方法
  30. #只有调用本模块的时候main()方法才生效
  31. if __name__ == ‘__main__‘:
  32. main()
  33. #实例化
  34. target = Target(‘aaa‘, 123)
  35. print ‘name is: ‘, target.name
  36. print ‘data is: ‘, target.data
  37. #打印__doc__属性
  38. print ‘target.__doc__ is: ‘, target.__doc__
  39. #打印类__dict__属性
  40. print ‘Target.__dict__ is: ‘, Target.__dict__
  41. #打印静态变量
  42. print ‘staticVar is: ‘, Target.staticVar
  43. #打印内建函数dir()
  44. print ‘dir() is: ‘, dir(Target)
  45. #调用静态方法
  46. Target.foo()
  47. #调用类方法
  48. Target.bar()


From for notes (Wiz)

Python Object-oriented (top)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.