A detailed approach to object-oriented programming in Python (top) _python

Source: Internet
Author: User
Tags instance method

Creating a Class

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

Copy Code code as follows:

Class ClassName (Bases):
' Class documentation string ' # ' Classes document Strings '
Class_suite #类体

instantiation of

Instantiate a class by a class name followed by a pair of parentheses

Copy Code code as follows:

MC = MyClass () # Instantiate class initialization classes
' Int () ' constructor

def __int__ (self):
Pass


Note: Self is similar to the Java this keyword action, which refers to a reference to its own instance

Class properties

Python attributes are different from object-oriented languages such as Java and C + +, and Python attributes include data members and function elements, which are accessed by the period symbol.

Special Data Built-in properties

C. Name Class C (String)
Document string for C.doc Class C
C.bases of all parent classes in Class C
C. Attributes of C.dict Class C
C. Module Class C Definition of modules (new version 1.5)
C. class instance C corresponding classes (only in the new class)

Special method Builtin Properties

Dir (): Gets a list of class attributes or instance property names.

Static variable Properties

Defined directly in the class scope

Copy Code code as follows:

Class C (object):
foo = 100

Instance Variable Properties

Python's instance properties are different from Java and C + +. In Java and C + +, instance properties must first be declared/defined, while Python instance properties are dynamically created. 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
The init () of the device is one of the key points for setting these properties.

Copy Code code as follows:

def __init__ (self, Name, data):
Self.name = Name
Self.data = "123"

Note: Self is similar to the Java this keyword action, which refers to a reference to its own instance

Method Properties

It is divided into instance method and class method. The instance method belongs to only one instance, and the class method belongs to the class and all instances.

Instance method

Copy Code code as follows:

Class MyClass (object):
def mynoactionmethod (self):
Pass

Note: Self is similar to the Java this keyword action, which refers to a reference to its own instance

static method

Static methods are class-level methods that can be invoked directly without instantiating a class. There are two ways to define

adorners (commonly used)

Copy Code code as follows:

@staticmethod
def foo ():
print ' Call static method '

Built-in functions
Copy Code code as follows:

def foo ():
print ' Call static method '
foo = Staticmethod (foo) #静态方法

Class method

A static method is a class-level method that, unlike a static method, must display an incoming CLS class parameter, and if you need to call other static methods in a class, or a function of a class method, define a class method. Similar to static methods, there are also two methods of definition.

adorners (commonly used)

Copy Code code as follows:

@classmethod
def bar (CLS):
print ' Call class method and Access static varible (Staticvar): ', Cls.staticvar

Built-in functions
Copy Code code as follows:

def bar (CLS):
print ' Call class method and Access static varible (Staticvar): ', Cls.staticvar
Bar = Classmethod (bar) #类方法

detailed examples
Copy Code code as follows:

#!/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"

'''
You can define a static method with an adorner
@staticmethod
def foo ():
print ' Call static method '
'''
def foo ():
print ' Call static method '
foo = Staticmethod (foo) #静态方法

'''
You can use adorners to define class methods
@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 () method only takes effect
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 ()

Output

Copy Code code as follows:

This is a test function
Init instance
Name Is:aaa
Data is:123
target.__doc__ Is:this is Target definition
Target.__dict__ is: {' __module__ ': ' __main__ ', ' foo ': <staticmethod object at 0x7f3fd9310cc8>, ' Bar ': <classmet Hod object at 0x7f3fd9310d38>, ' Staticvar ': ' v1.0 ', ' main ': <function main at 0x7f3fd930e758>, ' __doc__ ': ' This I s Target definition ', ' __init__ ': <function __init__ at 0x7f3fd930e6e0>}
Staticvar is:v1.0
Dir () is: [' __doc__ ', ' __init__ ', ' __module__ ', ' Bar ', ' foo ', ' main ', ' Staticvar ']
Call static method
Call class method and Access static varible (Staticvar): v1.0

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.