PHP头条
热点:

Python类成员继承重写如何实现 Python类成员继承重写实现代码


Python类成员继承重写如何实现?本篇文章小编给大家分享一下Python类成员继承重写实现代码,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

类成员的继承和重写

成员继承:子类继承了父类除构造方法外的所有成员

方法重写:子类可以重新定义父类中的方法,这样就会覆盖父类中的方法,也称为重写

代码如下

class Person:

  def __init__(self,name,age):
    self.name = name
    self.__age = age

  def say_age(self):
    print('我的年龄:',self.__age)

  def say_introduce(self):
    print('我的名字是{0}'.format(self.name))

class Student(Person):
  def __init__(self,name,age,score):
    Person.__init__(self,name,age)
    self.score = score

  def say_introduce(self):
    print('不是,我的名字叫做{0}'.format(self.name))

s = Student('Xujie',18,70)
s.say_age()
s.say_introduce()

结果

Python类成员继承重写如何实现 Python类成员继承重写实现代码

  • 上一篇: java读取resources下文件方式如何实现 java读取resources下文件方式实现代码

  • 下一篇: MySQL死锁产生原因以及解决方法

www.phpzy.comtrue/php/39932.htmlTechArticlePython类成员继承重写如何实现 Python类成员继承重写实现代码 Python类成员继承重写如何实现?本篇文章小编给大家分享一下Python类成员继承重写实现代码,小编觉得挺不错的,现在分享...

相关文章

    暂无相关文章

PHP之友评论

今天推荐