一、继承
1.单继承
  • 一个对象使用另一个对象的属性和方法,被继承的类也称父类

(1)父类与子类的方法不一样

class Four():
def sub(self,x,y):
return x + y class Five(Four): #Five类继承了Four类 --> Five 类拥有了 Four 类下的所有函数方法
def reduce(self,a,b):
return a - b print (Five().sub(2,5)) #结果如下
7

(2)子类拥有与父类相同的方法

  • 当子类拥有与父类相同的方法,通过子类实例调用该方法后,执行的是子类下的方法
class Mother():
def name(self):
print("This is my mother!") class MySelf(Mother):
#对父类方法重写
def name(self):
print("My name is XiaoMing") M = MySelf()
M.name() #结果如下
My name is XiaoMing

(3)子类拥有与父类相同的方法和属性

class Teacher():
#在父类中定义属性
def __init__(self,name):
self.name = name
def Name(self):
print("My teacher name is {} !".format(self.name)) class MySelf(Teacher):
#对父类方法重写
def Name(self):
print("My name is {} !".format(self.name)) M = MySelf("XiaoWang")
M.Name() #结果如下
My name is XiaoWang !
"""
在子类中使用了 super() 函数调用父类方法(常用于多继承)
"""
class Teacher():
#在父类中定义属性
def __init__(self,name):
self.name = name
def Name(self):
print("My teacher name is {} !".format(self.name)) class MySelf(Teacher):
def __init__(self,course,name):
super(MySelf, self).__init__(name)
self.course = course
#对父类方法重写
def Name(self):
print("My name is {} !".format(self.name))
def Course(self):
print("我的{}课老师的名字是{}".format(self.course,self.name)) M = MySelf("数学","Bob")
M.Name()
M.Course() #结果如下
My name is Bob !
我的数学课老师的名字是Bob

2.多继承

  • 多重继承就是一个子类继承多个父类
class Mother():
def hobby(self):
print("Mother love shopping!") class Father():
def work(self):
print("Father work is Test Engineer") class Myself(Father,Mother):
pass M = Myself()
M.work()
M.hobby() #结果如下
Father work is Test Engineer
Mother love shopping!
class Mother():
def __init__(self,something):
self.something = something def Hobby(self):
print("Mother love {}!".format(self.something)) class Father():
def __init__(self,work):
self.work = work def Work(self):
print("Father work is {}".format(self.work)) class Myself(Father,Mother):
def __init__(self,work,something):
# 注意:对于多继承来说,使用 super() 只会调用第一个父类的属性方法
# 要想调用特定父类的构造器只能使用 "父类名.__init__(self)" 方式。如下:
Father.__init__(self, work)
Mother.__init__(self,something) M = Myself("test", "shopping")
M.Work()
M.Hobby()
#我们可以用mro来查看顺序
print(Myself.mro()) #结果如下
Father work is test
Mother love shopping!
[<class '__main__.Myself'>, <class '__main__.Father'>, <class '__main__.Mother'>, <class 'object'>]
  • 如果不同的两个父类出现了相同名称的属性或者方法,子类会继承谁的属性或者方法?
class Mother():
def __init__(self,work):
self.work = work
def hobby(self):
print("My mother work is {}.".format(self.work)) class Father():
def __init__(self,work):
self.work = work def hobby(self):
print("My father work is {}.".format(self.work)) class Myself(Father,Mother):
pass M = Myself("Test")
M.hobby() #结果如下
My father work is Test.
#由上面实例可知如下

(1)python3中都是新式类:广度优先,从父类中查询对应的方法,查询到第一个满足的方法之后就直接返回
object
|
A(object)
|
A_1(A) --> A_2(A)
|
Test(A_1, A_2) (2)python2中的经典类:深度优先
A
|
A --> A_2(A)
|
A_1(A)
|
Test(A_1, A_2)

python(类继承)的更多相关文章

  1. python 类继承演示范例的代码

    把做工程过程重要的代码片段备份一次,下面的资料是关于python 类继承演示范例的代码. # a simple example of a class inheritance # tested with ...

  2. python类继承

    面向对象的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过 继承 机制.继承完全可以理解成类之间的 类型和子类型 关系. 假设你想要写一个程序来记录学校之中的教师和学生情况.他们有一些 ...

  3. Python 类继承,__bases__, __mro__, super

    Python是面向对象的编程语言,也支持类继承. >>> class Base: ... pass ... >>> class Derived(Base): ... ...

  4. Python类继承(转发)

    目录 一.概述 二.类的继承 2.1 继承的定义 2.2 构造函数的继承 2.3 子类对父类方法的重写 三.类继承的事例 回到顶部 一.概述 面向对象编程 (OOP) 语言的一个主要功能就是“继承”. ...

  5. 第7.7节 案例详解:Python类继承机制

    本节实现一个类继承的小程序,下面一边结合代码一边介绍相关继承的知识.例子以车.汽车为例,车为父类.汽车为子类. 一.    定义父类Vehicle class Vehicle():    def __ ...

  6. python 类 - 继承

    继承 什么是继承? 编写类时,并非总要从空白开始.如果要编写的类是另一个现成类的特殊版本,可使用继承. 一个类继承另一个类时,将自动获得另一个类的所有属性和方法.现有的类称为父类,而新类称为子类. 子 ...

  7. python类继承的重写和super

    给已经存在的类添加新的行为,继承是非常好的实现方式.但是如果要改变行为呢?比如在Python继承扩展内置类,我们的contact类只允许一个名字和一个邮箱,但是如果要对某些人增加电话号码呢?这里可以通 ...

  8. python 类继承与子类实例初始化

    From: https://blog.csdn.net/cs0301lm/article/details/6002504?utm_source=blogxgwz4 [ 先贴参考书籍原文(中文英文对照) ...

  9. python 类继承

    #!/usr/bin/python # Filename: inherit.py class SchoolMember: '''Represents any school member.''' def ...

  10. Python类继承,方法重写及私有方法

    # -*- coding: utf-8 -*- """ Created on Mon Nov 12 15:05:20 2018 @author: zhen "& ...

随机推荐

  1. Linux 压缩备分篇(一 备份数据)

    备份文件                dump dump: -S                    仅列出待备份数据需要多少磁盘空间才能够备份完毕 -u                    将 ...

  2. public、private、protected继承区别

  3. python基础入门:matplotlib绘制多Y轴画图(附源码)

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:屁屁酱 PS:如有需要Python学习资料的小伙伴可以加点击下方链接 ...

  4. F - Distinct Numbers

    链接:https://atcoder.jp/contests/abc143/tasks/abc143_f 题解:开两个数组,其中一个arr用来保存每个元素出现的次数,同时再开一个数组crr用来保存出现 ...

  5. K - Downgrade Gym - 101775K

    题目大意:一天不玩相当于A-B中将A转换为经验值,B舍弃掉,然后A=1,在通过升级所需要的经验值来判断可以升几级 题目连接:https://codeforces.com/gym/101775/prob ...

  6. A Bug's Life POJ 2492

    D - A Bug's Life 二分图 并查集 BackgroundProfessor Hopper is researching the sexual behavior of a rare spe ...

  7. PHP出现SSL certificate:unable to get local issuer certificate的解决办法

    当本地curl需要访问https时,如果没有配置证书,会出现SSL certificate: unable to get local issuer certificate错误信息. 解决办法: 1.下 ...

  8. Springboot:员工管理之公共页面提取 高亮显示(十(5))

    把顶部和左侧的公共代码分别放到header.html和left.html中 顶部代码:resources\templates\header.html 主内容展示: <!DOCTYPE html& ...

  9. SpringCloud(一)学习笔记之项目搭建

    [springcloud项目名称不支持下划线] 一.创建父项目 File---new---project: 填写项目信息: 默认即可,点击finish创建完成: 由于父项目只用到pom文件 所以把sr ...

  10. java实现自定义哈希表

    哈希表实现原理 哈希表底层是使用数组实现的,因为数组使用下标查找元素很快.所以实现哈希表的关键就是把某种数据类型通过计算变成数组的下标(这个计算就是hashCode()函数 比如,你怎么把一个字符串转 ...