ImportRandom as RclassFish:def __init__(self): self.x= R.randint (0, 10) Self.y= R.randint (0, 10) defMove (self): self.x-= 1Print('My pos is:', self.x, SELF.Y)classGoldfish (Fish):PassclassCarp (Fish):PassclassSalmon (Fish):PassclassShark (Fish):def __init__(self):#overriding the parent class __init__ () methodFish.__init__(self)#Inheriting Method 1, you need to specify the parent class nameSuper ().__init__()#Inheritance Method 2, does not need to specify the parent class name, will automatically findSelf.hungry =TruedefEat (self):ifself.hungry:Print('Eat everyday!') Self.hungry=FalseElse: Print("I can ' t eat anymore!")
Two methods of Python inheritance:
1. Classname.funcname (args ...)
2, Super (). FuncName ()
1 classBase1:2 deffunc1 (self):3 Print("func1")4 5 classBase2:6 defFunc2 (self):7 Print("Func2")8 9 classC (Base1, BASE2):#Multiple InheritanceTen Pass
Multiple inheritance adds the class name to inherit directly in the class name parameter.
Python inheritance//Multiple inheritance