About... In the code in this article self The meaning of , You can refer to the following blog post :
https://blog.csdn.net/wenhao_ir/article/details/125384347
class Fruit:
color = 'red'
def taste(self):
return 'delicious'
apple = Fruit()
a_character = apple.color
b_character = apple.taste()
print('\na_character:{}'.format(a_character))
print('b_character:{}'.format(b_character))
The results are shown in the following figure :
class Staff: # Staff For the employees
bonus = 30000 # bonus For allowance 、 Bonus means
def salary(self): # salary It means salary
salary = 10000+self.bonus
return salary
zhang_san = Staff()
zhang_san_salray = zhang_san.salary()
The running results are as follows :
class Staff: # Staff For the employees
def __init__(self, bonus): # bonus For allowance 、 Bonus means
self.bonus = bonus
def salary(self): # salary It means salary
salary = 10000+self.bonus
return salary
zhang_san = Staff(3000)
zhang_san_salray = zhang_san.salary()
The operation results are as follows :
class AnimalBaseClass:
def __init__(self, num):
self.animallegs = num
self.head_num = 0
def walk(self):
print(' go ')
def cry(self):
print(' It's called ')
def get_legs_num(self):
print(self.animallegs)
def input_head_num(self, num2):
self.head_num = num2
def get_head_num(self):
print(self.head_num)
animal = AnimalBaseClass(4)
animal.input_head_num(1)
animal.get_legs_num()
animal.get_head_num()
In the above code , Member functions input_head_num() With parameters .
The operation results are as follows :
class AnimalBaseClass:
def __init__(self, num):
self.animallegs = num
def walk(self):
print(' go ')
def cry(self):
print(' It's called ')
def get_legs_num(self):
print(self.animallegs)
class BirdClass(AnimalBaseClass):
head_num = 1
def cry(self):
print(' Squeak ')
def run(self):
print(' run ')
def get_head_num(self):
print(self.head_num)
piyo_suke = BirdClass(2)
piyo_suke.cry()
piyo_suke.run()
piyo_suke.get_legs_num()
piyo_suke.get_head_num()
In the code above , class BirdClass Inherited from class AnimalBaseClass, It has classes AnimalBaseClass Variables and member functions of .
The operation results are as follows :
When both parent and child classes have member functions with the same name , When a subclass calls this member function, it will call the member function in the subclass first , This function is called rewriting ( Cover ), In English, it is called “override” So the statements in the above code
piyo_suke.cry()
The run result of is “ Squeak ”, instead of “ It's called ”.
It is not just general member functions that can be rewritten , Constructors can also be overridden in subclasses , Consider the following code :
class AnimalBaseClass:
def __init__(self, num):
self.animallegs = num
def walk(self):
print(' go ')
def cry(self):
print(' It's called ')
def get_legs_num(self):
print(self.animallegs)
class BirdClass(AnimalBaseClass):
def __init__(self, num1, num2):
self.animallegs = num1
self.head_num = num2
def cry(self):
print(' Squeak ')
def run(self):
print(' run ')
def get_head_num(self):
print(self.head_num)
piyo_suke = BirdClass(3, 1)
piyo_suke.cry()
piyo_suke.run()
piyo_suke.get_legs_num()
piyo_suke.get_head_num()
In the above code , Subclass BirdClass Constructor and superclass of AnimalBaseClass Dissimilarity .
The operation results are as follows :
it is to be noted that , What member variables are used in your code , You should pay attention to initializing the constructor , Otherwise, an error will occur during the call , For example, the following is not enough :
class BirdClass(AnimalBaseClass):
def __init__(self, num2):
self.head_num = num2
def cry(self):
print(' Squeak ')
def run(self):
print(' run ')
def get_head_num(self):
print(self.head_num)
piyo_suke = BirdClass(1)
piyo_suke.cry()
piyo_suke.run()
piyo_suke.get_legs_num()
piyo_suke.get_head_num()
In the above code , Member functions get_legs_num() Member variables are used animallegs, But no initialization , So in the call statement piyo_suke.get_legs_num() The following mistakes will be reported :
Pay attention to the following points in the writing format of the code ( Not necessary , But the code written in this way is beautiful and standard ):