class SubClass (SuperClassl , SuperClass2 , … ):
#Class definition part
class Animal:
def dog(self):
Print ('I have a dog! His name is% s'% self.dog_name)
class Child:
def son(self):
Print ('I have two sons!')
#Define Myself class, inherit Animal class and Child class
class Myself(Animal,Child):
def name(self):
Print ('My name is Xiaoming! ')
M=Myself() # Create Myself object
M. Dog_name='rhubarb' # Add dog_name class variable through Myself object
M. Dog () # Call the dog () method of Myself object to print that I have a dog! His name is rhubarb
M. Son() # Call the son() method of Myself object to print that I have two sons!
M. Name() # Call the name() method of Myself object to print my name as Xiao Ming!
class Animal:
def name(self):
Print ('I have a dog, his name is rhubarb!')
class Child:
def name(self):
Print ('I have two sons!')
class Myself(Animal,Child):
pass
class Yourself(Child,Animal):
pass
M = Myself()
M. Name() # Print that I have a dog named Dahuang!
Y = Yourself()
Y. Name() # Print that I have two sons!
class Animal:
def name(self):
Print ('I have a dog, his name is rhubarb!')
class Myself(Animal):
#Override the name() method of the Animal class
def name(self):
Print ('I don't have a dog, I only have a cat, his name is Dabai!')
M=Myself() # Create Myself object
M. Name() # Execute the name() method of Myself object to print that I don't have a dog, I only have a cat, and his name is Da Bai!
class Animal:
def name(self):
Print ('I have a dog, his name is rhubarb!')
class Myself(Animal):
#Override the name() method of the Animal class
def name(self):
Print ('I have a cat, his name is Dabai!')
def pet(self):
Animal. name (self) # Call the overridden parent class method name(), using the class name. instance name. You need to manually pass self
Self. name() # Execute the name() method and call the name() method overridden by the subclass
M=Myself() # Create Myself object
M.pet()
”’
I have a dog, his name is rhubarb!
I also have a cat, his name is Dabai!
”’
class Animal:
def __init__(self,pet,name):
self.pet = pet
self.name = name
def favourite_animal(self):
Print ('I have a% s, its name is% s!'% (self.pet,self.name))
class Fruit:
def __init__(self,kind):
self.kind = kind
def favourite_fruit(self):
Print ('My favorite fruit is% s!'% self.kind)
class Myself(Animal,Fruit):
pass
M=Myself ('dog ',' rhubarb ') # Create Myself object
M. Favorite_animal() # Call the favorite_animal() method of the Myself object to print that I have a dog named rhubarb!
M. Favorite_fruit() # Call the favorite_fruit method of Myself object. Because the kind instance variable of Fruit object is not initialized, an error is reported AttributeError: 'Myself' object has no attribute 'kind'
>>> help(super)
Help on class super in module builtins:
class super(object)
| super() -> same as super(__class__, <first argument>)
| super(type) -> unbound super object
| super(type, obj) -> bound super object; requires isinstance(obj, type)
| super(type, type2) -> bound super object; requires issubclass(type2, type)
| Typical use to call a cooperative superclass method:
| class C(B):
| def meth(self, arg):
| super().meth(arg)
| This works for class methods too:
| class C(B):
| @classmethod
| def cmeth(cls, arg):
| super().cmeth(arg)
…
class Animal:
def __init__(self,pet,name):
self.pet = pet
self.name = name
def favourite_animal(self):
Print ('I have a% s, its name is% s!'% (self.pet,self.name))
class Fruit:
def __init__(self,kind):
self.kind = kind
def favourite_fruit(self):
Print ('My favorite fruit is% s!'% self.kind)
class Myself(Animal,Fruit):
def __init__(self,pet,name,kind):
Fruit. __init__ (self, kind) # Call parent class construction method through unbound method
super().__ Init__ (pet, name) # Call the parent class construction method through the super() function
M=Myself ('Dog ',' Rhubarb ',' Apple ')
M. Favorite_animal() # Print that I have a dog named rhubarb!
M. Favorite_fruit() # Print that my favorite fruit is an apple!