当前位置:主页 > python教程 > Python类继承重写

Python类成员继承重写的实现

发布:2021-04-19 08:55:11 59


本站精选了一篇Python类相关的编程文章,网友范孟夏根据主题投稿了本篇教程内容,涉及到Python、类成员、继承、重写、Python类继承重写相关内容,已被936网友关注,涉猎到的知识点内容可以在下方电子书获得。

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()

结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持码农之家。


参考资料

相关文章

网友讨论