Home > other >  Python: to expand the superclass method
Python: to expand the superclass method

Time:09-15

The class Animal:

Def eat (self) :
Print (" eat -- - ")

Def ultimately responds (self) :
Print (" drink - ")

Def run (self) :
Print (" run -- - ")

Def sleep (self) :
Print (" sleep -- - ")


The class Dog (Animal) :

Def bark (self) :
Print (" bark ")


The class XiaoTianQuan (Dog) :

Def fly (self) :
Print (" I fly ")

Def bark (self) :

# 1. According to the need of specific to subclass, writing code
Print (" god the same call...
")
# 2. Use the super () call was encapsulated in the parent class method
Super (). Bark ()

# 3. Add other subclass code
Print (" $% % ^ ^ * $% ^ # % $% ")


XTQ=XiaoTianQuan ()

# if the subclasses, rewrite the superclass method
# when using subclass object method is called, is called a subclass of rewriting method
XTQ. Bark ()


If XiaoTianQuan class fly method invokes the bark of the parent class method, can also how to write in the fly method (do not use super ())?


Helpless non-computer majors, elective python, puzzling over an inconclusive, seeking big help,

CodePudding user response:

Def fly (self) :
T dark (self)
Print (XXXXXXX)

CodePudding user response:

Do, is not recommended if you change the class name of the Dog class, will be expected to change in all subclasses of all calls, increases the difficulty to modify
  • Related