Python from introduction to practice: Chapter 9 learning notes

Posted by vsego on Mon, 24 Jan 2022 04:09:16 +0100

Python from introduction to practice: Chapter 9 learning notes

Chapter 9: Category
Create and use classes

# Create Dog class

class Dog():
	"""A simple operation to simulate a dog"""
	def __init__(self, name, old):   #Notice the color of init here
		"""initialization name and old attribute"""
		self.name = name
		self.old = old

	def set(self):  #Variables prefixed with self can be used by all methods in the class, although this method uses self Name, but you don't need to add its formal parameters here.
		print("The dog is sitting down!" + self.name.title())

	def roll_over(self):
		print("The dog is rolling!" + self.name.title())

The functions in a class are called methods__ init__ () is a special method. Whenever you create a new instance according to the Dog class, Python will run it automatically. The formal parameter self is essential and must be in front of other formal parameters. It is a reference to the instance itself, so that the instance can access the properties and methods in the class.
**Variables prefixed with self can be used by all methods in the class, * * self Name = name gets the value stored in the formal parameter name and stores it in the variable name. Then the variable is associated with the currently created instance, which is called an attribute.
The Dog class also defines two other methods: sit() and roll_over(). Since these methods do not require additional information, such as name or age, they have only one formal parameter, self.

Create real columns based on classes

my_dog = Dog('jim', 6)

print("My dog's name is " + my_dog.name.title())
print("My dog's old is " + str(my_dog.old))
#My dog's name is Jim
#My dog's old is 6

Represents the real column of a Dog, which is stored in the variable my_ In Dog, note: we can generally think that capitalized names (such as Dog) refer to classes, while lowercase names (such as my_dog) refer to instances created from classes.
Access the properties of the real column. The period indicates: my_dog.name
Call the method in the class. The period indicates: my_dog.sit()

(1) Assign default values to properties
When setting the default value, in the method__ init__ () designated within; If you do this for a property, you do not need to include a formal parameter that provides an initial value for it.

class Car():
	def __init__(self, name, make, year):
		"""Set properties. If a property has a default value, there is no need to set formal parameters!"""
		self.name = name
		self.make = make
		self.year = year
		self.odometer_reading = 0  #There is no formal parameter corresponding to it

(2) Modify the value of the property
Modify by real column: real column name Attribute = xx
Modify: instance name by method Method (xx)

Inheritance:
When writing classes, you don't always start with a blank space. If the class you want to write is a special version of another ready-made class, you can use inheritance. When a class inherits from another class, it will automatically obtain all properties and methods of the other class; The original class is called the parent class, and the new class is called the child class. A subclass inherits all the properties and methods of its parent class, and can also define its own properties and methods.

class Car():
   ---------
# After the parent class is written, write the child class
class ElectriCar(Car):
	def __init__(self, name, make, year):
		"""Initializes the properties of the parent class"""
		super().__init__(name, make, year)

super() is a special function that helps Python associate parent and child classes

#Materialization
my_car = ElectriCar('audo', 'china', 2017)
message = my_car.describe_car()
print(message)
#2017 Audo china

(1) Define new properties and methods for subclasses

# Add some new properties to the subclass
class ElectriCar(Car):
	def __init__(self, name, make, year):
		"""Initializes the properties of the parent class"""
		super().__init__(name, make, year)
		"""Add new properties for subclasses"""
		self.battery_size = 70

	def show_battery_size(self):
		print("The battery size of this new tram is " + str(self.battery_size))

your_car = ElectriCar('audo', 'china', 2017)
your_car.show_battery_size()
#The battery size of this new tram is 70

(2) Re: the method of the parent class can be overridden as long as it does not conform to the behavior of the real object simulated by the child class. For this purpose, you can define such a method in the subclass, which has the same name as the parent method to be overridden,

(3) Real columns as properties
In practice, you may encounter more and more class details. You need to extract a part of the large class as a separate small class, and then use the instance of this small class as an attribute of the large class.

#Parent class
class User():
	"""Store basic user information"""
	def __init__(self, first_name, last_name, school, old):
		"""Set the properties first"""
		self.first_name= first_name
		self.last_name = last_name
		self.school = school
		self.old = old
		self.login_attempts = 0

	def greet_user(self):
		print("Hello, little " + self.last_name+ 'greatly~~')

# Write classes as properties
class Privilege():
	def __init__(self):
		self.privilege_1 = 'Change the earth'

	def show_privileges(self):
		print("The administrator's ability is" + self.privilege_1)
#Subclass
class Admin(User):
	def __init__(self, first_name, last_name, school, old):
		"""Initializes the properties of the parent class"""
		super().__init__(first_name, last_name, school, old)
		"""Add new properties for subclasses privilegeļ¼ŒAnd take the class as an attribute"""
		self.privilege = Privilege()

user_1 = Admin("element", "Xiaoyu", "Nameless University", '22')
#Call materialized user_ Method in the property privilege of 1
user_1.privilege.show_privileges()  #The administrator's ability is to change the earth