Python basic grammar learning--class


foreword

The learning content this time is mainly object-oriented classes.


1. Create and use classes

The following is a simple definition of a class. There are no parentheses after Dog, indicating that this class does not inherit from other classes and is created from blank.

>>> class Dog:
	def __init__(self,name,age):
		self.name = name
		self.age = age
	def sit(self):
		print("{name} is sit".format(name = self.name))

The __init__() method is a magic method. There are three formal parameters in this method, self, name, age, among which self cannot be missing, because when calling this class to create an instance, the init method will be called automatically, and then the The actual parameter self is automatically passed in, and each method call related to the instance will automatically pass the actual parameter self, which is a reference to the instance itself, allowing the instance to access the properties and methods in the class.

The sit() method is an ordinary method with only one formal parameter self, indicating that the created instance can call this method.

dog_1 = Dog('deng',18)
dog_1.sit()

The above is to create an instance dog_1 of the Dog class, and only pass in two actual parameters name and age, and self will be passed in automatically. Then call the sit() method

Sometimes it is also possible to specify a default value for some properties.
For example

>>> class Dog:
	def __init__(self,name,age):
		self.name = name
		self.age = age
		self.number = 100
	def sit(self):
		print("{name} is sit".format(name = self.name))

>>> dog_1 = Dog('deng',18)
>>> dog_1.number
100
>>> 

When defining this class, specify the value of the number attribute as 100, create an instance of this class, and print out the value of this default attribute.

2. Inheritance

When writing a class, you don't have to create it from scratch, you can choose to inherit a parent class.
When a class inherits from a parent class, it gets all the properties and methods of the parent class. After the subclass inherits all the properties and methods of the parent class, it can also define its own properties and methods.

class Animal:
	def __init__(self,name,age):
		self.name = name
		self.age = age
	def sit(self):
		print("sittint")

An Animal class is defined here

 class Dog(Animal):
	def __init__(self,name,age,number):
		super().__init__(name,age)
		self.number = number

Then define a Dog class that inherits from the Animal parent class. In the __init__ method of the subclass, the first super().__init__(name,age)function is to call the __init__ method of the parent class, so that the subclass contains all the attributes defined in this method. It can be seen that the subclass Dog() has one more attribute number than the parent class Animal(), so next assign a value to the number attribute.

If the method of the parent class is not suitable for the subclass, you can also choose to override the method of the parent class, just write a method name with the same name as the parent class in the subclass.

Guess you like

Origin blog.csdn.net/weixin_47250738/article/details/130665702