Python object-oriented (1) _ preliminary understanding

Object-Oriented

Two object-oriented features compared with a process-oriented programming

1, reducing the reusability of code.
2, enhance the readability of the code.

Object-oriented programming advantage

1 is a collection of classes that function similar functions to make your code more clarity, more rationalization.
2. Object-oriented, to have God's perspective look at the problem, the object is instantiated from a particular template.

Class structure

class is the same keyword def usage, define a class.
Human is a kind of class names, class names use camel (CamelCase) naming style, capitalized private classes available a leading underscore.
Class structure of the general direction to be divided into two parts: the
static variable.
Dynamic method

Static properties of the class name Operation

First, all of the content view class: class name .__ dict__ way.
Second, the universal point by point out the parameters or the way you want

Dynamic operation

Provided: In addition to two special methods: Method to static class method, the method generally does not operate by the class name of a class.

class Human:
    """
    此类主要是构建人类
    """
    mind = '有思想'  # 第一部分:静态属性 属性 静态变量 静态字段
    dic = {}
    l1 = []
    def work(self): # 第二部分:方法 函数 动态属性
        # print(self)
        print('人类会工作')
    def tools(self):
        print('人类会使用工具')

Human.work(111)
Human.tools(111)
下面可以做,但不用。
Human.__dict__['work'](111)

Object Description

The object is out of the class, as long as the class name with (), which is an example of the process, this will instantiate an object.

Instantiate an object total, there were three things

1, opened up an object space in memory.
2, class __init__ method automatically executed, and the target space (memory address) passed __init__ method first position parameter self.
3, in the __init__ method add attributes to the object space through the self.

The method of operation of the object class

The method generally by class (class methods out of the outer, static methods) on the object, and the object implementation of these methods are self parameter automatically passed to the method in object space.

What self is?

in fact, self class method (function) in the first position parameter, but the interpreter will automatically call this function of the object pass self. So we first argument of the class convention provided self, is representative of this object.

A plurality of class objects can be instantiated

Guess you like

Origin www.cnblogs.com/SkyRabbit/p/11317667.html