• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

喡子

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

python类的继承与类的多态

类的继承:
所谓‘继承’从字面上看很好理解:如果一个类继承了另外一个类,则这个类就有了被继承类的所有方法和属性。
可以字节使用这些方法和属性,不需要单独再写

#-*-coding=utf-8-*-
class Parent(object):
def print_self(self):
return '我是父类'

class Student(Parent):
def __init__(self,name,score):
self.name=name
self.score=score
def print_score(self):
print('%s: %s' % (self.name,self.score))
a=Student(name='zhangsan',score=99)
print(a.score)
print(a.print_self())

 

 


类的多态:
如果子类继承了父类,但是又想改变父类的方法,这个时候不需要修改父类的方法,只需要在子类中添加相同的方法名就可以起到覆盖的作用;

 

 

 

 




posted on 2021-07-18 11:02  喡子  阅读(111)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3