多彩编程 多彩编程MZPH · CODE BLOG
ARTICLE DETAIL

文章详情

深耕前端与后端开发技术的一线实战笔记与踩坑复盘。

Python 面向对象进阶——继承、多态、魔术方法

Python 面向对象进阶——继承、多态、魔术方法 一、继承classAnimal:def__init__(self,name):self.namenamedefspeak(self):passclassDog(Animal):defspeak(self):returnf{self.name}说汪汪二、多态animals[Dog(小黑),Cat(小白)]forainanimals:print(a.speak())# 调用各自的实现三、魔术方法classStudent:def__init__(self,name,score):self.namename self.scorescoredef__str__(self):returnfStudent({self.name},{self.score})def__repr__(self):returnself.__str__()def__lt__(self,other):returnself.scoreother.score 觉得有用的话点赞 关注【张老师技术栈】吧
返回列表