1. Create Classes
Create a dog class based on the puppy, and each instance will store the name and age, how to give each puppy the ability to eat and run:
Parse as follows:
1.class Dog: In python, uppercase names refer to classes, and class definitions do not have parentheses;
2. The comments section describes the main functions of this class;
3. Special methods_ init_ () is defined as having three parameters: self, name, and age. Each time a new instance is created from a Dog class, python runs with two underscores at the beginning and end.
4. self.name and self. Both name variables have self, and variables prefixed with self can be used by all methods in a class, and variables such as this that can be accessed through an instance are called attributes.
5. Dog also has two methods eat() and run(), which do not require additional information to execute and therefore have only one parameter, sdle.
class Dog: # This is a domain class def __init__(self, name, age): # Initialization properties name and age self.name = name self.age = age def eat(self): # Ways to eat print(f"{self.name}Eat something....") def run(self): # Running Method print(f"{self.name}Run....")
2. Create instances from classes
2.1 Access Properties
1. Create a puppy named "pipi pi", aged 6
2.my_dog.name: access the properties of the instance, using the period notation
my_dog = Dog('pipi', 6) print(f"My puppy barks{my_dog.name}") print(f"It's this year{my_dog.age}Aged.")
Run result:
My puppy barks pipi It is six years old this year.
2.2 Call Method
Once you have created an instance from the Dog class, you can use the period notation to invoke the methods defined in the Dog class
my_dog.eat() my_dog.run()
Run result:
pipi Eat something.... pipi Run....
2.3 Create multiple instances
Two instances named "pipi pi" and "maomao" were created in this class. Each puppy is a separate instance with its own properties and can perform the same operation.
my_dog = Dog('pipi', 6) your_dog = Dog('maomao',3) print(f"My puppy barks{my_dog.name}") print(f"It's this year{my_dog.age}Aged.") my_dog.eat() print('\n') # Output a line break for easy viewing print(f"Your puppy barks{your_dog.name}") print(f"It's this year{your_dog.age}Aged.") your_dog.eat()
Run result:
My puppy barks pipi It is six years old this year. pipi Eat something.... Your puppy barks maomao It is three years old this year. maomao Eat something...
Note: Even if the second puppy specifies the same name and age, python will still create another instance based on the Dog class.
3. Use Classes and Instances
3.1 Phone Class
Write a Phone class for your phone to store information about your phone, and a way to summarize that information
Here, the Phone class, the value of the parameter, and the method mean the same as in the previous Dog class example.
class Phone: """about iphone Classes of""" def __init__(self, brand, version, year): """Initialization phone Properties of""" self.brand = brand self.version = version self.year = year def get_descriptive_name(self): """Back to a neat description""" return f"{self.brand} {self.version} Particular year:{self.year}" my_phone = Phone('iphone', '12', 2020) print(my_phone.get_descriptive_name())
Run result:
iphone 12 Year: 2020
3.2 Assign default values to attributes
When creating an instance, some attributes do not need to be defined by parameters, but can be defined in method_u init_u () specify a default value for it.
An attribute for service time and how to use it is added below
class Phone: """about iphone Classes of""" def __init__(self, brand, version, year): """Initialization phone Properties of""" self.brand = brand self.version = version self.year = year self.service_time = 0 def get_descriptive_name(self): """Back to a neat description""" return f"{self.brand} {self.version} Particular year:{self.year}" def dis_time(self): """Show mobile usage time""" return f"The phone is already in use {self.service_time}Hours " my_phone = Phone('iphone', '12', 2020) print(my_phone.get_descriptive_name()) print(my_phone.dis_time())
After running, python creates a name named dis_time() method and set its initial value to the default of 0
iphone 12 Year: 2020 The phone has been in use for 0 hours
3.3 Modifying the value of an attribute
There are three ways to modify attributes: directly through an instance, set by a method, and increment by a method (increasing a specific value)
3.3.1 Modify the value of an attribute directly
my_phone = Phone('iphone', '12', 2020) print(my_phone.get_descriptive_name()) my_phone.service_time = 100 #Modify the value of an attribute directly print(my_phone.dis_time())
Run Results
iphone 12 Year: 2020 The phone has been in use for 100 hours
3.3.2 Modify the value of an attribute by method
Modify the method used in the class to add a parameter hour to it
def dis_time(self, hour): """Show mobile usage time""" self.service_time = hour return f"The phone is already in use {self.service_time}Hours " my_phone = Phone('iphone', '12', 2020) print(my_phone.get_descriptive_name()) print(my_phone.dis_time(200))
Call dis_ When using the time() method, provide the argument 200 as follows:
iphone 12 Year: 2020 The phone has been in use for 200 hours
Use if to determine if the user calls dis_later Is the argument given by the time () method normal because it should be used longer and longer and should not be callback? Modify dis_ The method for time () is as follows:
def dis_time(self, hour): """Show mobile usage time""" if hour >= self.service_time: self.service_time = hour return f"The phone is already in use {self.service_time}Hours " else: return "No time callback"
The second call passed in less arguments than the first run:
iphone 12 Year: 2020 The phone has been in use for 200 hours No time callback
3.3.3 Increasing the value of an attribute by a method
Sometimes you need to increment the value of an attribute by a specific amount instead of setting it to a new value, assuming that the first time you invoke it is 1000 hours, then all subsequent calls are incremental, increasing add_ The time () method is as follows:
def add_time(self, add_hour): self.service_time+=add_hour return f"The phone is already in use {self.service_time}Hours " my_phone = Phone('iphone', '12', 2020) print(my_phone.get_descriptive_name()) print(my_phone.dis_time(1000)) print(my_phone.add_time(100))
Test results:
iphone 12 Year: 2020 The phone has been in use for 1000 hours The phone has been in use for 1100 hours