Car class
classCar ():" "Car Simulator" " def __init__(self,name,model,year):" "Initialize the properties of the car" "Self.name=name Self.model=Model Self.year= YeardefGet_describe_car (self):" "return descriptive information" "Long_name= str (self.year) +' '+self.name +' '+Self.modelreturn(long_name)#Create an instanceMy_new_car = Car ('Audi','A4',' .')Print(My_new_car.get_describe_car ())
is a new car class, and we created the instance and stored it in My_new_car
Assigning a default value to a property
If you specify a default value in Method __init__ (), you do not need to include the parameter that provides the initial value for it
classCar ():" "Car Simulator" " def __init__(self,name,model,year):" "Initialize the properties of the car" "Self.name=name Self.model=Model Self.year=Year self.miters=0defGet_describe_car (self):" "return descriptive information" "Long_name= str (self.year) +' '+self.name +' '+Self.modelreturn(long_name)defprint_miters (self):Print("This car has"+ STR (self.miters) +"miles on it")#Create an instanceMy_new_car = Car ('Audi','A4',' .')Print(My_new_car.get_describe_car ()) my_new_car.print_miters ()
An initial value is defined in the code above, in which we do not pass in the value, but in method print_miters we use the property
Modifying the value of a property
Three ways to modify the value of a property:
① directly through the instance to modify
② By way of setting
③ increment by method (adds a specific value)
1. Modify with an instance
classCar ():" "Car Simulator" " def __init__(self,name,model,year):" "Initialize the properties of the car" "Self.name=name Self.model=Model Self.year=Year self.miters=0defGet_describe_car (self):" "return descriptive information" "Long_name= str (self.year) +' '+self.name +' '+Self.modelreturn(long_name)defprint_miters (self):Print("This car has"+ STR (self.miters) +"miles on it")#Create an instanceMy_new_car = Car ('Audi','A4',' .')Print(My_new_car.get_describe_car ()) My_new_car.miters= 100my_new_car.print_miters ()
2. Set by method
Without direct access to the property, the value can be passed to a method that is updated internally by him
classCar ():" "Car Simulator" " def __init__(self,name,model,year):" "Initialize the properties of the car" "Self.name=name Self.model=Model Self.year=Year self.miters=0defGet_describe_car (self):" "return descriptive information" "Long_name= str (self.year) +' '+self.name +' '+Self.modelreturn(long_name)defupdate_miters (self,miters): Self.miters=mitersdefprint_miters (self):Print("This car has"+ STR (self.miters) +"miles on it")#Create an instanceMy_new_car = Car ('Audi','A4',' .')Print(My_new_car.get_describe_car ()) My_new_car.update_miters (200) my_new_car.print_miters ()
3. Use the method to increment a specific amount of the property value instead of setting it to a completely new value
classCar ():" "Car Simulator" " def __init__(self, name, model, year):" "Initialize the properties of the car" "Self.name=name Self.model=Model Self.year=Year self.miters=0defGet_describe_car (self):" "return descriptive information" "Long_name= str (self.year) +' '+ Self.name +' '+Self.modelreturn(long_name)defupdate_miters (self,miters): Self.miters=mitersdefincrement_miters (self,add_miters):" "set the mileage to the specified number" "self.miters+=add_mitersdefprint_miters (self):Print("This car has"+ STR (self.miters) +"miles on it")#Create an instanceMy_user_car = Car ('Audi','A4',' .')Print(My_user_car.get_describe_car ()) My_user_car.update_miters (200) my_user_car.print_miters () my_user_car.increment_miters (100) my_user_car.print_miters ()
The new Method Increment_miters () takes a number and adds it to Update_miters () the number of miles that have been modified, we call the method Update_miters () to pass in 200, and then call Increment_miters ( Added 100 on the 200 basis and then hit it out.
"Python" uses classes and instances