Python面向对象程序设计之继承、多态原理与用法详解
作者:随风行云 时间:2023-07-08 18:17:52
本文实例讲述了Python面向对象程序设计之继承、多态原理与用法。分享给大家供大家参考,具体如下:
相关内容:
继承:多继承、super、__init__、重写父类变量或函数
多态
继承:
在Python3中,不写基类的类默认继承object
继承就是子类获得了父类的全部功能:比如学生和老师都有“姓名,性别,年龄、ID”等学校人员属性,如果学生和老师都直接继承学校人员的“姓名,性别,年龄、ID”,那么就不必要在学生类和老师类中重复定义了。
继承指定类的方式是:class 类名(基类名)
class SchoolPerson:
def study(self):
print("it's time to study")
class Student(SchoolPerson):#继承的方法
pass
a=Student()
a.study()
----------------------
结果:
it's time to study
上述结果显示,Student继承了SchoolPerson中的study函数
python支持多继承:class SubClassName (ParentClass1[, ParentClass2, ...]):
对象对于属性【包括变量和函数】的查找路径:本实例对象--》本类--》基类【如果有多个基类,从左到右到基类中查找】
可以调用基类的__init__方法:
方式有两种:
,建议使用super,避免错误使用多次基类的__init__
class SchoolPerson:
def __init__(self,name,age,sex,mid):
self.name=name
self.age=age
self.sex=sex
self.mid=mid
def study(self):
print("it's time to study")
class Student(SchoolPerson):#继承的方法
def __init__(self,name,age,sex,mid,grade):
# super(Student,self).__init__(name,age,sex,mid)
SchoolPerson.__init__(self,name,age,sex,mid)
self.grade=grade
def study(self):
print("i am student,i want to study")
a=Student('A',18,'m','001',1)
a.study()
print(a.name,a.age,a.mid,a.grade)
对于多继承中的调用基类的__init__:无论多少个基类,也只会调用一次基类的__init__
可以覆盖父类中属性【变量、函数】:
class SchoolPerson:
def __init__(self,name,age,sex,mid):
self.name=name
self.age=age
self.sex=sex
self.mid=mid
def study(self):
print("it's time to study")
class Student(SchoolPerson):#继承的方法
def __init__(self,name,age,sex,mid,grade):
super(Student,self).__init__(name,age,sex,mid)
self.grade=grade
def study(self):
print("i am student,i want to study")
a=Student('A',18,'m','001',1)
a.study()
--------------------------
结果:
i am student,i want to study
多态:
python是否具有多态具有争议性。
但不论是否具有,这并不影响我们的具体使用。
class Animal:
def __init__(self, name):
self.name = name
def talk(self):
pass
@staticmethod ####如果硬要说多态,建议使用静态方法来处理,而非普通函数
def animal_talk(obj):
obj.talk()
class Cat(Animal):
def talk(self):
print('喵!')
class Dog(Animal):
def talk(self):
print('wang')
d = Dog("小黑")
c = Cat("小白")
# def animal_talk(obj):
# obj.talk() ###这也是一个能实现功能的函数,因此python多态才具有争议性
Animal.animal_talk(c)
Animal.animal_talk(d)
希望本文所述对大家Python程序设计有所帮助。
来源:https://www.cnblogs.com/progor/p/8419973.html
标签:Python,面向对象,继承,多态
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python 敏感词过滤的实现示例
2021-07-04 12:17:28
Python类的定义、继承及类对象使用方法简明教程
2021-09-01 21:43:10
MySQL分类排名和分组TOP N实例详解
2024-01-24 11:23:16
![](https://img.aspxhome.com/file/2023/3/106603_0s.png)
pandas 实现分组后取第N行
2023-02-09 11:11:40
![](https://img.aspxhome.com/file/2023/5/75295_0s.jpg)
python dict乱码如何解决
2023-04-11 06:43:51
golang如何修改json文件内容的方法示例
2024-04-26 17:32:44
Js 按照MVC模式制作自定义控件
2008-10-12 12:11:00
python GUI库图形界面开发之PyQt5线程类QThread详细使用方法
2023-12-03 20:29:40
![](https://img.aspxhome.com/file/2023/1/70221_0s.png)
vue中的路由传值与重调本路由改变参数
2024-04-27 16:10:12
![](https://img.aspxhome.com/file/2023/9/132969_0s.png)
Python灰度变换中灰度切割分析实现
2021-10-05 12:07:49
![](https://img.aspxhome.com/file/2023/0/97320_0s.png)
pytorch实现ResNet结构的实例代码
2022-12-31 01:50:48
![](https://img.aspxhome.com/file/2023/5/89405_0s.jpg)
Sql Server事务语法及使用方法实例分析
2024-01-22 12:23:38
![](https://img.aspxhome.com/file/2023/7/94457_0s.png)
百度编辑器二次开发常用手记整理小结
2023-10-22 20:28:33
Python实现简单猜拳游戏
2022-07-08 04:40:10
![](https://img.aspxhome.com/file/2023/0/124210_0s.jpg)
python最长回文串算法
2023-03-05 02:27:37
Python3 Tkinter选择路径功能的实现方法
2022-03-12 19:39:28
![](https://img.aspxhome.com/file/2023/0/85170_0s.jpg)
Oracle收购TimesTen 提高数据库软件性能
2010-07-21 13:03:00
JS循环中正确使用async、await的姿势分享
2024-05-25 15:18:56
![](https://img.aspxhome.com/file/2023/2/123712_0s.gif)
python生成带有表格的图片实例
2023-05-22 03:30:16
![](https://img.aspxhome.com/file/2023/4/81474_0s.jpg)
Python中字典创建、遍历、添加等实用操作技巧合集
2021-04-02 08:22:12