python 16.1 继承 重写 supper()方法 多重继承
一、继承
1、功能:继承提高了代码的复用性 ,让类与类之间产生了关系,有了这个关系,才有了多态的特性。
2、形式 class B(A):pass ,创建B类继承A
3、说明:1、在创建类的时候,如果我们省略父类则默认父类为object,所有类都继承object,object是所有类的父类。
class Animals():def __init__(self,name):self._name = namedef run(self):print('i can run ')class Cat(Animals):def scratch_mouse(self):print('i can catch jerry')c = Cat('tom')print('im %s'%c._name)
c.run()
c.scratch_mouse()
im tom
i can run
i can catch jerry
二、 重写
如果子类中有父类同名的方法,则通过子类的实例去调用该方法时,会调用子类的方法而不是父类的方法,这个特点也称为方法的重写(覆盖),就近原则。
class Animals():def __init__(self,name):self._name = namedef run(self):print('animals can run ')class Cat(Animals):def run(self):print('cat can run')def scratch_mouse(self):print('i can catch jerry')c = Cat('tom')print('im %s'%c._name)
c.run()
c.scratch_mouse()
im tom
cat can run
i can catch jerry
三、 supper()方法
super()可以获得当前类的父类,通过super()返回对象调用父类方法时,不需要传递self。
一个错误:
class Animals():def __init__(self,name):self._name = namedef run(self):print('animals can run ')class Cat(Animals):def run(self):print('%s can run'%_name)def scratch_mouse(self):print('i can catch jerry')c = Cat('tom')print('im %s'%c._name)
c.run()
c.scratch_mouse()
im tomFile "C:/Users/yylwin10/Desktop/16.1 继承 重写.py", line 19, in <module>c.run()File "C:/Users/yylwin10/Desktop/16.1 继承 重写.py", line 11, in runprint('%s can run'%_name)
NameError: name '_name' is not defined
原因:子类中看不到父类中的属性和方法!
解决方法1:
class Animals():def __init__(self,name):self._name = namedef run(self):print('animals can run ')class Cat(Animals):def run(self):print('%s can run'%c._name)def scratch_mouse(self):print('i can catch jerry')c = Cat('tom')print('im %s'%c._name)
c.run()
c.scratch_mouse()
这样写虽然实现了功能,但是这样很不好。
方法二“:在父类中提供一个获取name的接口,在子类中利用super()方法获取父类。
class Animals():def __init__(self,name):self._name = namedef run(self):print('animals can run ')#@propertydef name(self):return self._nameclass Cat(Animals):def run(self):print('%s can run'%super().name())def scratch_mouse(self):print('%s can catch jerry'%super().name())c = Cat('tom')print('im %s'%c._name)
c.run()
c.scratch_mouse()
im tom
tom can run
tom can catch jerry
方法三:利用super在子类中调用父类的__init__ 方法。
class Animals():def __init__(self,name):self._name = namedef run(self):print('animals can run ')class Cat(Animals):def __init__(self,name):super().__init__(name)def run(self):print('%s can run'%self._name)c = Cat('tom')print('im %s'%c._name)
c.run()
im tom
tom can run
四、多重继承
Python中支持多重继承,也就是我们可以为一个类同时指定多个父类,开发时尽量不要用多重继承,原因是增加代码的复杂程度,如果多个父类中出现重名的方法,则会先在第一个父类中寻找,然后在找第二个,在找第三个,自己类中的方法会覆盖父类中的同名方法。
__bases__ 方法,语法 类名.__bases__ 可以获取当前类所有的父类
class A(object):def text(self):print('A中的text方法')class B(object):def text(self):print('B中的text方法')def text2(self):print('B中的text2方法')class C(A,B):def text2(self):print('C中的text2方法')c = C()
c.text()
c.text2()
print(C.__bases__)
A中的text方法
C中的text2方法
(<class '__main__.A'>, <class '__main__.B'>)
python 16.1 继承 重写 supper()方法 多重继承相关推荐
- Dart中类的静态方法,继承,重写父类方法
一.静态成员和静态方法 class Person {//1.在变量或者方法最前面增加static关键字就是静态方法或者静态属性了; //2.静态方法和静态属性只能使用类才能进行调用,相当于OC中的加号 ...
- python oop 继承_关于oop:使类数据在python中可继承的正确方法是什么?
我是Python的新手,来自Perl领域. 我正在尝试确定关于类数据的存储和访问的最佳实践,以便它可以被子类继承并可能进行扩展.阅读Python 2.7文档(我确实使用2.6),深入研究Python和 ...
- python类似于java的重写toString方法
python类中的特殊方法: __str__(self)用来重写 people类: class People:'所有员工的基类'#构造函数def __init__(self, user_id, use ...
- Python 在子类中调用父类方法详解(单继承、多层继承、多重继承)
Python 在子类中调用父类方法详解(单继承.多层继承.多重继承) by:授客 QQ:1033553122 测试环境: win7 64位 Python版本:Python 3.3.5 代码实践 ...
- python类方法继承_对python中类的继承与方法重写介绍
1.单继承 父类也叫基类 子类也叫派生类 如下所示,继承的关系: 继承的书写格式: class 子类(父类): 方法 实例: class Animal: def eat(self): print(&q ...
- python进阶16多继承与Mixin
原创博客链接: python进阶16多继承与Mixin Mixin解释 为了让大家,对这个 Mixin 有一个更直观的理解,摘录了网上一段说明. 民航飞机是一种交通工具,对于土豪们来说直升机也是一种交 ...
- 什么是python中子类父类_零基础入门:python中子类继承父类的__init__方法实例
前言: 今天为大家带来的内容是零基础入门:python中子类继承父类的__init__方法实例!具有不错的参考意义,希望在此能够帮助到各位!(喜欢的话记得点赞转发关注不迷路哦) 使用Python写过面 ...
- Python 重写父类方法
python继承中,如果子类在调用某个方法时,它首先是从派生类(也就是当前类)中去找对应的方法,如果当前类中找不到对应的方法,就会去基类(派生类)中去逐个查找. 若父类的方法不能满足子类的需要,那么子 ...
- python在多继承中子类调用某个父类的同名方法之 super讲解(mro机制)***
目录 2. 钻石继承遇到的难题 3. 针对钻石问题各语言的解决方法 3.1. C++ 3.2. Java 3.3. Ruby 3.4. Python 4. super的内核:mro 5. ...
最新文章
- 《超越需求:敏捷思维模式下的分析》—第1章 1.2节交付价值
- java 静态成员不能调用 非静态成员_为什么静态成员不能访问非静态成员
- 全渠道的核心是渠道协同和数据整合
- 线程池之ScheduledThreadPoolExecutor线程池源码分析笔记
- 数据结构——基于 Dijsktra 算法的最短路径求解
- 福州联通与市政府携手 共筑新型智慧城市
- rhcs集群套件—红帽6的高可用
- python实现qq登录界面_使用Python编写一个QQ办公版的图形登录界面!
- 【杂文】【演化学习】platEMO提取缓存Data
- python---pass和continue和break和exit()区别
- 【JY】结构概念之(消能减震黏滞阻尼器)
- 远心镜头与普通镜头拍摄工件孔径对比
- 干货:Spark RDD写入HBase 优化
- linux 的 ip 命令 和 ifconfig 命令
- 如何判断出连接到电脑的USB设备是手机?
- linux设置密码报错automa,AutomaThemely:根据太阳日出及日落时间自动切换明暗的Gtk主题...
- 计组—运算器实验报告1最全分析总结
- java智慧停车_GitHub - read125/example-parking: 基于BIM智慧停车的室内定位示例
- Microsoft Office 2016 专业增强版官方原版镜像下载
- Android 10.0 软硬键盘同时使用的兼容(软键盘与内置物理键盘共存)