We understood that object-oriented programming (OOP Object Oriented Programming)

We understood that object-oriented programming (OOP: Object Oriented Programming)

Object-oriented and process-oriented

What is the process-oriented programming?

Answer: According to first do, and then what, what's the last thought. Things yourself, lines, programmers are wage earners perspective

Pros: the simplification of complex issues, streamline

Disadvantages: poor scalability

What is Object-Oriented Programming?

A: In object-oriented programming, the programmer is like creating a world, in this world, the programmer has created a target, to help solve the problem yourself. Things are subject to dry, the boss programmer perspective.

Advantages: scalability

Disadvantages: slightly to the complexity of the process-oriented

The concept of objects and classes

Object:

  • Wherein (variable) and skills (function) combination.
  • The object is a specific existence of things

class:

  • The same characteristics (variable) number of objects and skills (function.) Combination
  • Class is an abstract concept

In the real world: is it a specific object, and then these objects have the same characteristics and skills attributed to a class. For example: some people this is the first object and then everyone comes down to this abstract concept of humanity.

In the program: the first is the definition of a class, then the class, instantiated as an object.

class

In one class, the most common definition of the variables and functions. Focus: body class code will be executed in the definition phase, and then generates a class name space, the name of the class body of code produced, thrown into the class namespace.

Class functions inside the body of code only when called before the execution, ** but, like inside __init__ function is executed at the point of definition. **

#查看类名称空间里面的名称:__dict__
class Student:
        school = '社会大学'
    def __init__(self, name):
        self.name = name
        
    def study(self):
        print('我是学生,我会学习')
 
print(Student.__dict__) 
'''
结果:{'__module__': '__main__', 'school': '中北大学', '__init__': <function Student.__init__ at 0x108e685f0>, 'study': <function Student.study at 0x108e684d0>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__doc__': None}

school、study、__init__: 类定义时产生的名称
__module__:当前文件的模块,因为是执行文件,所以是__main__
__doc__:类的文档注释
'''

print(Student.school)  #结果:社会大学  #相当于:print(Student.__dict__['school'])
print(Student.study)  
#结果: <function Student.study at 0x1013244d0>  #相当于:print(Student.__dict__['study'])
Student.school = '北京大学'  # 相当于Student.__dict__['school'] = '北京大学'

(****) class is essentially a namespace, which kept the name of variables and functions, is a storage container of variables and functions

One use of class: find name from the namespace

The use of the two categories: green Object

Variable defined in a class is shared by all objects, if the class has changed the variables, all the objects. When this variable will change. Objects can not change the variable type of object. A class variable, did not change the class of this variable, in essence, is to add a namespace attribute in their own object.

class Student:
    age = 18
    def __init__(self):
        pass
      
stu1 = Student()
stu2 = Student()
print(stu1.age) # 18
print(stu2.age) # 18
Student.age = 10
print(stu1.age) # 10
print(stu2.age) # 10

stu1.age = 20
print(stu1.age)  # 20
print(stu2.age)  # 10
print(stu1.__dict__)  # {'age': 20} 

Objects

Call the class can produce objects.

After the call the return value is called an object class / instance.

Procedure Call the class is called instantiating the class.

Nature of the object also form a namespace. If the class is no init parameter, namespace object is empty.

例1:
class Student:
        def __init__(self):
        print(1)
        
stu = Student()
print(stu.__dict__)  #结果:{}

例2:
class Student:
    def __init__(self, name):
        self.name = name
        
 stu = Student('你爹')
print(stu.__dict__)  #结果:{'name':'你爹'}

Focus: What happened to call the class?

1, generates an empty object returns

2, the class init trigger execution of the function, this empty object of the first parameter passed to init. Calling class parameters within parentheses again after the first pass parameter init parameters.

init supplement: 1, init function is called when the class will be executed. 2, init function does not return a value.

Property search order

Object. Attribute. Attribute called back

Object. When property, now named spatial objects inside to find there is no such property, if not, to find the namespace attribute from the class inside.

class Student:
    school = '中北大学'
    age = 18
    def __init__(self, name):
        self.name = name

    def study(self):
        print(1)

stu = Student('你爹')
print(stu.name)  
print(stu.age)
'''
说明:stu的命名空间只有name, stu.__dict__ = {'name':'你爹'}, 没有age,这时候就在类的名称空间中找。
类名称空间在类定义的时候有:school, age, __init__, study等,有age,则返回18
'''

Binding approach

1, class variables may be defined in the class, the object can also be used. No matter who calls, all point to the same address. Once the class variable values ​​change, both change along with all object calls.

class Student:
    age = 18
    def __init__(self):
        pass
stu = Student()
print(id(stu.age))  # 4362244432
print(id(Student.age)) # 4362244432

Student.age = 20
stu1 = Student()
print(stu.age)  # 20
print(stu1.age) # 20

2, defined in the class of functions, classes can also be used, when the class calls, this function is an ordinary function. Function defined in the class is used for objects. And it is used to bind to the object.

What is binding to the target?

class Student:
    school = 'oldboy'

    def __init__(self, name, age, sex):
        self.name = name #stu1.name='小白'
        self.age = age   #stu1.age=18
        self.sex = sex   #stu1.sex='male'

    def choose_course(self,x): #self=stu1
        print('%s choosing course' %self.name)
        
stu1=Student('小白',18,'male')  # 小白 choosing course
stu2=Student('小花',38,'female') # 小花 choosing course
stu3=Student('小黑',28,'male') # 小黑 choosing course
'''
如上,用类实例化了三个对象,三个对象分别调用类里面的函数。调用时,会自动把对象传给类函数的第一个参数。
也就是说,类方法分别绑定给了不同的对象,哪个对象调用,用到的就是哪个对象的名称空间。不同对象互不干扰。
'''

Guess you like

Origin www.cnblogs.com/KbMan/p/11241406.html