1. Create the class, set the property and set the default value for the property, set the method and access the class's properties;
2, the use of classes to create multiple instances, as well as the method of invoking the class two ways;
3. Set the function that updates the properties, and update the properties of the instance.
1 classDog (object):2 """Create puppy Dog class"""3 4 def __init__(self, Name, age):5 """initialization parameters, when Python creates an instance, automatically passes in the argument self, pointing to the instance itself to access properties and methods"""6Self.name =name7Self.age = Age8 " "Assigning a default value to a property" "9Self.color =' White'Ten One defsit (self): A " "accessing the properties of a class" " - Print(Self.name.title () +'is now siting.') - the defRoll (self): - Print(Self.name.title () +'is now rolling') - - " "Modifying the value of a property by method" " + defUpdateColor (self,color): -Self.color =str (color) + Print(Self.name.title () +"' s color is"+str (color)) A at " "method One creates an instance by assigning a value to the parameter before it is brought into the class parameter, and then calls the methods of the class" " -Name = input ("Input your dog ' s name:\n") -Age = Input ("Input your dog ' s age:\n") -Jacksdog =Dog (name,age) - " "This invokes the method of the class, bringing the instance into the class as a parameter" " - dog.sit (Jacksdog) in Dog.roll (Jacksdog) - " "Method one Output: to ************************ + Input your dog ' s name: - Jack the Input your dog ' s age: * Ten $ Jack is now siting.Panax Notoginseng Jack is now rolling - ************************" " the + " "the method has the same principle, but the variable is directly assigned by the input." " ATomsdog = Dog (Input ("Input your dog ' s name:\n"), Input ("Input your dog ' s age:\n")) the " "the method of invoking the instance directly here" " + " "multiple instances have been created" " - tomsdog.sit () $ Tomsdog.roll () $Tomsdog.updatecolor (Input ("What ' s your dog ' s color:\n")) - " "Method, Output: - ************************ the Input your dog ' s name: - TomWuyi Input your dog ' s age: the 9 - Tom is now siting. Wu Tom is now rolling - What ' s your dog ' s color: About Yellow $ Tom ' s color is yellow - ************************" "
python--the creation and application of a simple class