Python类继承重写 Python类成员继承重写的实现

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python类继承重写 Python类成员继承重写的实现

Xujie_0528   2021-03-16 我要评论
想了解Python类成员继承重写的实现的相关内容吗,Xujie_0528在本文为您仔细讲解Python类继承重写的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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()

结果

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们