Connected to python learning notes (2)
1, Object oriented Foundation
1. Object oriented understanding
Object oriented is to regard programming as a thing. For the outside world, things are used directly, regardless of their internal situation. Programming is to set what things can do. Object oriented is used to simplify code
2. Classes and objects
1. What are classes and objects, and what are their relationships?
How is the washing machine produced?
Drawing – > washing machine – > laundry
In the process of object-oriented programming, there are two important components: classes and objects
Class is equivalent to drawing, and object is equivalent to washing machine
Relationship between class and object: use class to create (instantiate) an object
2. Class
Class is a general term for a series of things with the same characteristics and behavior. It is an abstract concept, not something that really exists. There are variables and functions in the class.
object
Objects are real things created by classes, such as washing machines
Note: in development, there are classes before objects
3. Object oriented implementation method
3.1 definition class
Syntax:
class Class name(): code ...... Note: the class name shall meet the identification naming rules and follow the naming habit of big hump
3.2. Create object
Syntax:
Object name=Class name()
3.3 experience class and object
Demand: wash clothes, function: can wash clothes
#1. Define washing machine class class Washer(): def wash(self): print("Can wash clothes") #2. Create object #Object name = class name () haier=Washer() #3. Verification results #3.1. Print haier object print(haier) #3.2. Use wash function --- instance method / object method --- usage: object name wash() haier.wash() The result is: <__main__.Washer object at 0x0000019A17E52358> Can wash clothes
3.4 self in class
In the above example, when we define a function, the input parentheses directly pop up self. What is this self?
self refers to the object that calls the function
The code is as follows:
class Waher(): def wash(self): print("wash clothes") print(self) #The result of printing is the memory address of the haier object haier=Waher() print(haier) haier.wash() The result is: <__main__.Waher object at 0x0000022543D42358> wash clothes <__main__.Waher object at 0x0000022543D42358>
3.5. Create multiple objects in one class
When a class creates multiple objects and multiple objects call functions, is the self address the same?
The verification code is as follows:
class Washer(): def wash(self): print("wash clothes") print(self) haier1=Washer() haier1.wash() haier2=Washer() haier2.wash() The result is that a class can create multiple objects. When multiple objects call functions, self The values are different wash clothes <__main__.Washer object at 0x00000277DD9652E8> wash clothes <__main__.Washer object at 0x00000277DD965358>
3. Add and get object properties
Attributes are features, such as the width, height, weight of the washing machine
Object properties can be added and obtained both outside the class and inside the class
1. Add object properties outside the class
Syntax: object name Attribute name = value
give an example:
haier.width=500 haier.height=800
2. Get object properties outside the class
Syntax: object name Attribute name
If users want to see the results, they must use print to output the results
The code is as follows:
haier.width=500 haier.height=800 print("haier What is the width of the washing machine{},Height is{}".format(haier.width,haier.height)) The result is: haier The width of the washing machine is 500,The height is 800
3. Get object properties in class
Syntax: self Attribute name
class Washer(): def wash(self): #Class to get instance properties print(f"haier What is the width of the washing machine{self.width}") print(f"haier What is the height of the washing machine{self.height}") #create object haier=Washer() #Add instance properties haier.width=500 haier.height=800 haier.wash() The result is: haier The width of the washing machine is 500 haier The height of the washing machine is 800
4. Magic method
In python__ xx__ The function of () is called magic method, which refers to the function with special function
1,_ init_()
Function: used to initialize objects
be careful:
1. When creating an object, it is called by default and does not need to be called manually
2,_ init _ The self parameter in (self) does not need to be passed by the developer. The python interpreter will automatically pass the current object reference
The code is as follows:
#Objective: define the init magic method, set the initialization properties, and access the call #1. Definition class: init magic method: width and height; Add instance method: access instance properties class Washer(): def __init__(self): self.width=400 self.height=500 def print_info(self): print(f"haier The width of the washing machine is{self.width},The height of the washing machine is{self.height}") #2. Create object haier=Washer() #3. Verification results haier.print_info() The result is: haier The width of the washing machine is 400,The height of the washing machine is 500
2. Parametric_ init _ ()
A class can create multiple objects. How to set different initialization properties for different objects?
The answer is: pass parameters
The code is as follows:
class Washer(): def __init__(self,width,height): self.width=width self.height=height def print_info(self): print(f"The width of the washing machine is{self.width}") print(f"The height of the washing machine is{self.height}") haier1=Washer(30,40) #Create multiple objects with different attribute values haier1.print_info() #Call instance method print("-"*20) haier2=Washer(50,60) haier2.print_info() The result is: The width of the washing machine is 30 The height of the washing machine is 40 ------------------- The width of the washing machine is 50 The height of the washing machine is 60
3,_ str_()
When using print to output an object, the memory address of the object is printed by default. If defined_ str_ Method, the data return ed in this method will be printed
The code is as follows:
class Washer(): def __init__(self,width,height): self.width=width self.height=height def print_info(self): print(f"The width of the washing machine is{self.width}") print(f"The height of the washing machine is{self.height}") def __str__(self): return "This is the instruction manual of Haier washing machine" haier=Washer(100,200) print(haier) haier.print_info() The result is: This is the instruction manual of Haier washing machine The width of the washing machine is 100 The height of the washing machine is 200
4,_ del_()
When an object is deleted, the python interpreter also calls by default_ del_ () method
The code is as follows:
class Washer(): def __init__(self): self.width=300 def __del__(self): print("The object has been deleted") haier=Washer() The result is: The object has been deleted
5. Comprehensive application
Demand 1: roasted sweet potato
1. Roasted time and corresponding sweet potato state:
0-3 minutes: raw
3-5 minutes: half cooked
5-8 minutes: cooked
More than 8 minutes: burnt
2. Seasoning added
Users add spices according to their own wishes
The analysis steps are as follows:
The implementation code is as follows:
class SweetPotato(): def __init__(self): #Baking time self.cook_time=0 #Roasted state self.cook_static="raw" #Seasoning list self.condiments=[] def cook(self,time): """Method for roasting sweet potato""" #Previous baking time + this baking time self.cook_time+=time if 0<=self.cook_time<3: self.cook_static="raw" elif 3<=self.cook_time<5: self.cook_static="halfcooked" elif 5<=self.cook_time<8: self.cook_static="Ripe" elif self.cook_time>=8: self.cook_static="It's burnt" def add_condiments(self,condiment): """Add seasoning""" self.condiments.append(condiment) def __str__(self): return f"This sweet potato is roasted{self.cook_time}Minutes, status is{self.cook_static},The spices added are{self.condiments}" digua=SweetPotato() digua.cook(4) digua.add_condiments("Honey") print(digua) digua.add_condiments("mustard") print(digua)
The result is:
Demand 2: move furniture
Place furniture smaller than the remaining area of the house into the house
Step analysis:
Demand involves two things: house and furniture, and two categories: house and furniture