Python中类继承和方法重写的示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python中类继承和方法重写的示例相关的知识,希望对你有一定的参考价值。

This simple example will show you how to inherit a class from a parent class. I have to apologise for some grammar mistakes that I've probably put in the comments, but English is not my native language.

If you execute this code, the output will be:


Here comes Lois Lane

Here comes Jimmy Olsen

Here comes Clark Kent

...but his secret identity is 'Superman' and he's a super-hero!


--> Let's see what a man can do:


Jimmy Olsen walks

Lois Lane says: 'Oh no, we're in danger!'

--> Let's see what a superman can do:


Clark Kent walks

Clark Kent says: 'This is a job for SUPERMAN!'

Superman run at the speed of light

Superman fly up in the sky

Superman uses his x-ray vision
  1. #!/usr/bin/env python
  2.  
  3. class man(object):
  4.  
  5. # name of the man
  6. name = ""
  7.  
  8. def __init__(self, P_name):
  9. """ Class constructor """
  10. self.name = P_name
  11. print("Here comes " + self.name)
  12.  
  13. def talk(self, P_message):
  14. print(self.name + " says: '" + P_message + "'")
  15.  
  16. def walk(self):
  17. """ This let an instance of a man to walk """
  18. print(self.name + " walks")
  19.  
  20. # This class inherits from Man class
  21. # A superman has all the powers of a man (A.K.A. Methods and Properties in our case ;-)
  22. class superman(man):
  23.  
  24. # Name of his secret identity
  25. secret_identity = ""
  26.  
  27. def __init__(self, P_name, P_secret_identity):
  28. """ Class constructor that overrides its parent class constructor"""
  29. # Invokes the class constructor of the parent class #
  30. super(superman, self).__init__(P_name)
  31. # Now let's add a secret identity
  32. self.secret_identity = P_secret_identity
  33. print("...but his secret identity is '" + self.secret_identity + "' and he's a super-hero!")
  34.  
  35. def walk(self, P_super_speed = False):
  36. # Overrides the normal walk, because a superman can walk at a normal
  37. # pace or run at the speed of light!
  38. if (not P_super_speed): super(superman, self).walk()
  39. else: print(self.secret_identity + " run at the speed of light")
  40.  
  41. def fly(self):
  42. """ This let an instance of a superman to fly """
  43. # No man can do this!
  44. print(self.secret_identity + " fly up in the sky")
  45.  
  46. def x_ray(self):
  47. """ This let an instance of a superman to use his x-ray vision """
  48. # No man can do this!
  49. print(self.secret_identity + " uses his x-ray vision")
  50.  
  51.  
  52. # Declare some instances of man and superman
  53. lois = man("Lois Lane")
  54. jimmy = man("Jimmy Olsen")
  55. clark = superman("Clark Kent", "Superman")
  56.  
  57. # Let's puth them into action!
  58.  
  59. print(" --> Let's see what a man can do: ")
  60. jimmy.walk()
  61. lois.talk("Oh no, we're in danger!")
  62.  
  63. print(" --> Let's see what a superman can do: ")
  64. clark.walk()
  65. clark.talk("This is a job for SUPERMAN!")
  66. clark.walk(True)
  67. clark.fly()
  68. clark.x_ray()

以上是关于Python中类继承和方法重写的示例的主要内容,如果未能解决你的问题,请参考以下文章

Python rest-framework 中类的继承关系(as_view)

Python中类的继承及类的属性和方法总结

python中类和对象的创建以及继承的实现

python中类和继承,继承还不太了解,先贴一段代码

Python类的继承和方法重写总结

python中类的多继承的搜索算法