1.继承引入,减少代码量

  1)版本1:

class Animal:
'''定义一个动物类'''
def eat(self):
print("----吃----")
def drink(self):
print("----喝----")
def sleep(self):
print("----睡觉----")
def run(self):
print("----跑----") class Dog:
def eat(self):
print("----吃----")
def drink(self):
print("----喝----")
def sleep(self):
print("----睡觉----")
def run(self):
print("----跑----")
def bark(self):
print("-----汪汪叫---") a = Animal()
a.eat() wangcai = Dog()
wangcai.eat()
----吃----
----吃----

  2)版本2:继承动物类

class Animal:
'''定义一个动物类'''
def eat(self):
print("----吃----")
def drink(self):
print("----喝----")
def sleep(self):
print("----睡觉----")
def run(self):
print("----跑----") class Dog(Animal):
'''
def eat(self):
print("----吃----")
def drink(self):
print("----喝----")
def sleep(self):
print("----睡觉----")
def run(self):
print("----跑----")
'''
def bark(self):
print("-----汪汪叫---") a = Animal()
a.eat() wangcai = Dog()
wangcai.eat()

  3)版本3:

class Animal:
'''定义一个动物类'''
def eat(self):
print("----吃----")
def drink(self):
print("----喝----")
def sleep(self):
print("----睡觉----")
def run(self):
print("----跑----") class Dog(Animal):
def bark(self):
print("-----汪汪叫---") class Cat(Animal):
def catch(self):
print("---抓老鼠---")
a = Animal()
a.eat() wangcai = Dog()
wangcai.eat() tom = Cat()
tom.eat()

    

2.子类继承父类的父类

class Animal:
'''定义一个动物类'''
def eat(self):
print("----吃----")
def drink(self):
print("----喝----")
def sleep(self):
print("----睡觉----")
def run(self):
print("----跑----") class Dog(Animal):
def bark(self):
print("-----汪汪叫---") class Xiaotq(Dog):
def fly(self):
print("---飞----") xiaotianquan = Xiaotq()
xiaotianquan.fly()
xiaotianquan.bark()
xiaotianquan.eat()
---飞----
-----汪汪叫---
----吃----

    

3.重写

先在自己的类中查找父类的同名方法,没有的话去父类查找

    

class Animal:
'''定义一个动物类'''
def eat(self):
print("----吃----")
def drink(self):
print("----喝----")
def sleep(self):
print("----睡觉----")
def run(self):
print("----跑----") class Dog(Animal):
def bark(self):
print("-----汪汪叫---") class Xiaotq(Dog):
def fly(self):
print("---飞----")
def bark(self): #定义和父类方法同名的方法,就是重写
print("---重写叫---") xiaotianquan = Xiaotq()
xiaotianquan.fly()
xiaotianquan.bark()
xiaotianquan.eat()
---飞----
---重写叫---
----吃----

4.调用被重写的方法

  既要调用父类的方法,又要重写该方法

  1)版本1: Dog.bark(self)

class Animal:
'''定义一个动物类'''
def eat(self):
print("----吃----")
def drink(self):
print("----喝----")
def sleep(self):
print("----睡觉----")
def run(self):
print("----跑----") class Dog(Animal):
def bark(self):
print("-----汪汪叫---") class Xiaotq(Dog):
def fly(self):
print("---飞----")
def bark(self):
print("---重写叫---") #第一种调用被重写的父类的方法
Dog.bark(self) xiaotianquan = Xiaotq()
xiaotianquan.fly()
xiaotianquan.bark()
xiaotianquan.eat()
---飞----
---重写叫---
-----汪汪叫---
----吃----

  2)版本2:super().dark()

class Animal:
'''定义一个动物类'''
def eat(self):
print("----吃----")
def drink(self):
print("----喝----")
def sleep(self):
print("----睡觉----")
def run(self):
print("----跑----") class Dog(Animal):
def bark(self):
print("-----汪汪叫---") class Xiaotq(Dog):
def fly(self):
print("---飞----")
def bark(self):
print("---重写叫---") #第一种调用被重写的父类的方法
# Dog.bark(self) #第2种
super().bark() #super()高级的,上级的 xiaotianquan = Xiaotq()
xiaotianquan.fly()
xiaotianquan.bark()
xiaotianquan.eat()

5.私有属性私有方法,不能被直接继承

私有属性私有方法可以被公有方法调用,公有方法可以被继承

  1)版本1:

 class A:
def __init__(self):
self.num1 = 100
self.__num2 = 222 def test1(self):
print("---test1---") def __test2(self):
print("--test2---") class B(A):
pass b = B()
b.test1()
b.__test2() #私有方法不会被继承 print(b.num1)
print(b.__num2) #私有属性不会被继承
---test1---
Traceback (most recent call last):
File "09-私有在继承中的表现.py", line 17, in <module>
b.__test2() #私有方法不会被继承
AttributeError: 'B' object has no attribute '__test2'

  2)版本2:

class A:
def __init__(self):
self.num1 = 100
self.__num2 = 222 def test1(self):
print("---test1---") def __test2(self):
print("--test2---") class B(A):
pass b = B()
b.test1()
#b.__test2() #私有方法不会被继承 print(b.num1)
#print(b.__num2) #私有属性不会被继承
---test1---
100

  3)版本3:通过公有方法调用

class A:
def __init__(self):
self.num1 = 100
self.__num2 = 222 def test1(self):
print("---test1---") def __test2(self):
print("--test2---") def test3(self):
self.__test2()
print(self.__num2) class B(A):
pass b = B()
b.test1()
#b.__test2() #私有方法不会被继承 print(b.num1)
#print(b.__num2) #私有属性不会被继承 b.test3() #公有方法调用私有属性方法
---test1---
100
--test2---
222

  4)版本4:

class A:
def __init__(self):
self.num1 = 100
self.__num2 = 222 def test1(self):
print("---test1---") def __test2(self):
print("--test2---") def test3(self):
self.__test2()
print(self.__num2) class B(A):
def test4(self):
self.__test2()
print(self.__num2) b = B()
b.test1()
#b.__test2() #私有方法不会被继承 print(b.num1)
#print(b.__num2) #私有属性不会被继承 b.test3()
b.test4()
---test1---
100
--test2---
222
Traceback (most recent call last):
File "09-私有在继承中的表现.py", line 29, in <module>
b.test4()
File "09-私有在继承中的表现.py", line 18, in test4
self.__test2()
AttributeError: 'B' object has no attribute '_B__test2'

    

6.多继承

经典类

 class Dog:
pass

新式类   object是所有类的父类

 class Dog(object):
pass

  

多继承

class Base(object):
def base(self):
print("----this is base-") class A(Base):
def test1(self):
print("---this is A---") class B(Base):
def test2(self):
print("----this is B---") class C(A,B):
pass c1 = C()
c1.test1()
c1.test2()
c1.base() 
---this is A---
----this is B---
----this is base-

    

7.多继承注意点

  切记:不要出现相同的方法名

  1)版本1:先调用自身的

class Base(object):
def test(self):
print("----this is base-") class A(Base):
def test(self):
print("---this is A---") class B(Base):
def test(self):
print("----this is B---") class C(A,B):
def test(self):
print("----this is C---") c1 = C()
c1.test()
----this is C---

  2)版本2:调用哪个父类的AorB??

 class Base(object):
def test(self):
print("----this is base-") class A(Base):
def test(self):
print("---this is A---") class B(Base):
def test(self):
print("----this is B---") class C(A,B):
pass
# def test(self):
# print("----this is C---") c1 = C()
c1.test()
---this is A---

  3)版本3:调用顺序

 class Base(object):
def test(self):
print("----this is base-") class A(Base):
def test(self):
print("---this is A---") class B(Base):
def test(self):
print("----this is B---") class C(A,B):
pass
# def test(self):
# print("----this is C---") c1 = C()
c1.test()
print(C.__mro__)

    

day 4 继承的更多相关文章

  1. javaScript的原型继承与多态性

    1.prototype 我们可以简单的把prototype看做是一个模版,新创建的自定义对象都是这个模版(prototype)的一个拷贝 (实际上不是拷贝而是链接,只不过这种链接是不可见,给人们的感觉 ...

  2. JavaScript的继承实现方式

    1.使用call或apply方法,将父对象的构造函数绑定在子对象上 function A(){ this.name = 'json'; } function B(){ A.call(this); } ...

  3. javascript中的继承与深度拷贝

    前言 本篇适合前端新人,下面开始...... 对于前端新手来说(比如博主),每当对js的对象做操作时,都是一种痛苦,原因就是在于对象的赋值是引用的传递,并非值的传递,虽然看上去后者赋值给了前者,他们就 ...

  4. 谈谈一些有趣的CSS题目(四)-- 从倒影说起,谈谈 CSS 继承 inherit

    开本系列,讨论一些有趣的 CSS 题目,抛开实用性而言,一些题目为了拓宽一下解决问题的思路,此外,涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题中有你感觉 ...

  5. JS继承类相关试题

    题目一: //有关于原型继承的代码如下:function Person(name) {   this.name = name;}Person.prototype = {     getName : f ...

  6. JS继承之寄生类继承

    原型式继承 其原理就是借助原型,可以基于已有的对象创建新对象.节省了创建自定义类型这一步(虽然觉得这样没什么意义). 模型 function object(o){ function W(){ } W. ...

  7. JS继承之借用构造函数继承和组合继承

    根据少一点套路,多一点真诚这个原则,继续学习. 借用构造函数继承 在解决原型中包含引用类型值所带来问题的过程中,开发人员开始使用一种叫做借用构造函数(constructor stealing)的技术( ...

  8. JS继承之原型继承

     许多OO语言都支持两种继承方式:接口继承和实现继承.接口继承只继承方法签名,而实现继承则继承实际的方法.如前所述,由于函数没有签名,在ECMAScript中无法实现接口继承.ECMAScript只支 ...

  9. 深入浅出JavaScript之原型链&继承

    Javascript语言的继承机制,它没有"子类"和"父类"的概念,也没有"类"(class)和"实例"(instanc ...

  10. 如果你也会C#,那不妨了解下F#(7):面向对象编程之继承、接口和泛型

    前言 面向对象三大基本特性:封装.继承.多态.上一篇中介绍了类的定义,下面就了解下F#中继承和多态的使用吧.

随机推荐

  1. psql: FATAL: role “postgres” does not exist

    I'm a postgres novice. I installed the postgres.app for mac. I was playing around with the psql comm ...

  2. jQuery UI 实例 – 切换(Toggle)

    toggle()函数用于为每个匹配元素的click事件绑定轮流的处理函数. toggle()是一个特殊的事件函数,用于为匹配元素的click事件绑定多个事件处理函数.每次触发click事件时,togg ...

  3. freemarker模板加载TemplateLoader常见方式

    使用过freemarker的肯定其见过如下情况: java.io.FileNotFoundException: Template xxx.ftl not found. 模板找不到.可能你会认为我明明指 ...

  4. vim在插入模式粘贴代码缩进问题解决方法

    转载自:https://blog.csdn.net/commshare/article/details/6215088 在vim粘贴代码会出现缩进问题,原因在于vim在代码粘贴时会自动缩进 解决方法: ...

  5. 如何使用jquery.qrcode.js插件生成二维码

    1.首先需要准备 jquery.qrcode.js 和 jquery.js github地址:https://github.com/lrsjng/jquery-qrcode 官方文档地址:http:/ ...

  6. 映射篇:request-String-Object-Map之间相互转化(程序员的成长之路---第5篇)

    为什么写这一篇 问题一:jdbc连接数据库返回的对象是ResultSet,如何把ResultSet对象中的值转换为我们自建的各种实体类? 我估计,80%的程序员会写jdbc数据库连接,但开发项目依然用 ...

  7. cocoscreator 2.04 配置 visual code 断点调试

    1,cocoscreator ,chrome浏览器,visual code 这三个软件的安装 2,官网配置visual code 环境 https://docs.cocos.com/creator/m ...

  8. PlanetLab介绍

    转自http://blog.sina.com.cn/s/blog_83517c050100vyzq.html PlanetLab产生背景 随着计算机技术和通信技术的不断发展,Internet的商业化和 ...

  9. C# Web Service简单使用

    第一步 打开VS,新建一个项目 第二步  创建一个ASP.NET 空 Web应用程序 我这里用的是VS2017 第三步 添加一个Web 服务(ASMX) 右键解决方案-->添加-->新建项 ...

  10. 转 Linux会话浅析(写得极好,表述清楚语言不硬)

    说起会话,我们经常登录到linux系统,执行各种各样的程序,这都牵涉到会话.但是,一般情况下我们又很少会去关注到会话的存在,很少会去了解它的来龙去脉.本文就对linux会话相关的信息做一些整理,看看隐 ...