Object Oriented Concept Summary
kind:
Abstraction of the characteristics and skills of a series of objects
How to understand: We abstract objects in real life into objects, such as students in school:
1. Categorization - We can group all students into a class of students.
2. Abstract the characteristics - what are the characteristics of the students? (school, name, gender, age)
3. Abstract Skills – What skills do students have? (study, eat, play, sleep)
A. When we abstract students into a class, we can generate students one by one by constructing classes:
B. Note: In the real world, we abstract classes through objects.
In the program, we must first define the class, in order to generate the object through the class.
# 1. First define the class abstractly from the real world # 2, and then generate objects through the class class Student: #Similar features school = ' luffycity ' #Similar skills def lean(self): print ( ' is leaning ' ) def eat(self): print('is eatting') def sleep(self): print('is sleeping') # 2. After the object is generated through the class stu1 = Student() #Instantiate a student object stu1 stu2 = Student() stu3 = Student() print(stu1) print(stu2) print(stu3)
Class usage:
Add, delete, modify and check:
#Add Student.local= ' Beijing ' #Add class # Delete del Student.local #Delete class # Change Student.school= ' Luffycity ' #Change class attributes #Check print ( Student. __dict__ ) #Check all print ( Student. school) #Check a single
The above case is to generate three objects with the same properties,
The __init__ method is used when we want to generate three different attributes
Example:
self : the instance itself, when instantiated, the python parser will automatically pass the instance itself through the self parameter.
If we want to instantiate multiple objects, we can pass the __init__() method and pass in the unique attributes of each object through the self parameter:
class LuffyStudent: #acquaintance characteristics school = ' luffycity ' #acquaintance skills def __init__ (self,name,sex,age): self.Name = name self.Sex=sex self.Age=age def lean(self): print('is leaning') def eat(self): print('is eatting') stu1 = LuffyStudent( ' Xiao He ' , ' Female ' ,22 ) stu2 = LuffyStudent( ' Little Li ' , ' Male ' ,23 ) stu3 = LuffyStudent( ' Little Red ' , ' Female ' ,22 ) print(stu1.Name)
Attributes:
The characteristics that objects have in common are called properties.
Such as the above code: school is a common feature.
method:
All the skills of an object are called methods.
Such as the above code: the common skills such as lean and eat are called methods.
Object:
As in the above code, by generating a class, an object is generated after instantiation
stu1=School() # This is called an object
#Object operation # Add stu1.local= ' Beijing ' #Add class # Delete del stu1.local #Delete class # Change stu1.school = ' Luffycity ' #Change class attributes #Check print ( stu1 . __dict__ ) #Check all print (stu1.school) #Check a single
Additional instructions:
1. From different angles, the defined classes are completely different
2. The class in reality is not exactly equal to the class in the program, and the class may be split in the program.
3. There can be classes in the program that do not exist in reality - strategy classes.