the python line 027 (object-oriented)

The concept of the object 1

  Objects can be thought of as a container, for containing data and functions, the concept of using the object data and the original dispersion functions integrated together, do both easy to use also improves the scalability of the program.

Class 2 with the object

  We can put the same function with the same type of object data stored in the class, each object without having to repeat a deposit, so just keep each object in its own unique data can greatly save space. So, if the object is used to store

Data container function, then the category is used to store the same data a plurality of container objects function.

  You must first define a class in the program, and then produce the object by calling the class.

3 Object-Oriented Programming

3.1 and the definition of class instantiation    

  Class naming body using the hump way, the code will execute in the class body of the class definition phase, which will have the name of a new name space used to store class definitions, you can print Student .__ dict__ like to see in full bloom in this container s things.

class Student: 
    School = ' UNIVERSITY OF SCIENCE ' 

    DEF the Choose (Self):
         Print (F ' {} are the self.name elective ' ) 

Print (Student. the __dict__ )

  Procedure call is referred to as the class of the instance of the class, is the return value of the get objects in the program, otherwise known as an example.

  Use __init __ () method can be customized object class is instantiated during the unique properties:

class Student: 
    School = ' UNIVERSITY OF SCIENCE ' 
    DEF  the __init__ (Self, name, Age, MALE): 
        the self.name = name 
        self.age = Age 
        self.gender = MALE 

    DEF the Choose (Self):
         Print (F ' {the self.name } is elective ' ) 

MRZ = Student ( ' MRZ ' , 18 is, ' MALE ' )
 Print (MRZ. the __dict__ )
 >>> { ' name ': ' Mar ' , ' age ' : 18, ' male ' : ' male ' }

3.2 property access

  3.2.1 class attributes and object attributes

  CRUD operations may be performed on the class attributes and object attributes.

  Find the order and binding method properties 3.2.2

  The name of the object in space only kept the unique properties of the object, and the object have similar attributes are stored in the class. The object, will give priority to look for when you access the properties of the object itself from __dict__, not found, then go to class __dict__ find.

Get the object class attribute id # is the same 
MRZ = Student ( 'MRZ', 18 is, 'MALE') mGy = Student ( 'mGy', 10, 'MALE') Print (ID (mrz.school)) Print (ID (mgy.school)) >>> 1502755346720 >>> 1502755346720

  Function definition class are mainly used to the subject, and is bound to the object, they are the same functionality, although all object points, it is bound to different objects of different binding methods, different memory addresses .

mrz = Student('mrz', 18, 'male')
mgy = Student('mgy', 10, 'male')
print(mrz.choose)
print(mgy.choose)
>>> <bound method Student.choose of <__main__.Student object at 0x00000208A55EFF98>>
>>> <bound method Student.choose of <__main__.Student object at 0x00000208A55F8080>>

 

Guess you like

Origin www.cnblogs.com/mmmmmrz/p/12654696.html