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

  1)版本1:

  1. class Animal:
  2. '''定义一个动物类'''
  3. def eat(self):
  4. print("----吃----")
  5. def drink(self):
  6. print("----喝----")
  7. def sleep(self):
  8. print("----睡觉----")
  9. def run(self):
  10. print("----跑----")
  11.  
  12. class Dog:
  13. def eat(self):
  14. print("----吃----")
  15. def drink(self):
  16. print("----喝----")
  17. def sleep(self):
  18. print("----睡觉----")
  19. def run(self):
  20. print("----跑----")
  21. def bark(self):
  22. print("-----汪汪叫---")
  23.  
  24. a = Animal()
  25. a.eat()
  26.  
  27. wangcai = Dog()
  28. wangcai.eat()
  1. ----吃----
  2. ----吃----

  2)版本2:继承动物类

  1. class Animal:
  2. '''定义一个动物类'''
  3. def eat(self):
  4. print("----吃----")
  5. def drink(self):
  6. print("----喝----")
  7. def sleep(self):
  8. print("----睡觉----")
  9. def run(self):
  10. print("----跑----")
  11.  
  12. class Dog(Animal):
  13. '''
  14. def eat(self):
  15. print("----吃----")
  16. def drink(self):
  17. print("----喝----")
  18. def sleep(self):
  19. print("----睡觉----")
  20. def run(self):
  21. print("----跑----")
  22. '''
  23. def bark(self):
  24. print("-----汪汪叫---")
  25.  
  26. a = Animal()
  27. a.eat()
  28.  
  29. wangcai = Dog()
  30. wangcai.eat()

  3)版本3:

  1. class Animal:
  2. '''定义一个动物类'''
  3. def eat(self):
  4. print("----吃----")
  5. def drink(self):
  6. print("----喝----")
  7. def sleep(self):
  8. print("----睡觉----")
  9. def run(self):
  10. print("----跑----")
  11.  
  12. class Dog(Animal):
  13. def bark(self):
  14. print("-----汪汪叫---")
  15.  
  16. class Cat(Animal):
  17. def catch(self):
  18. print("---抓老鼠---")
  19. a = Animal()
  20. a.eat()
  21.  
  22. wangcai = Dog()
  23. wangcai.eat()
  24.  
  25. tom = Cat()
  26. tom.eat()

    

2.子类继承父类的父类

  1. class Animal:
  2. '''定义一个动物类'''
  3. def eat(self):
  4. print("----吃----")
  5. def drink(self):
  6. print("----喝----")
  7. def sleep(self):
  8. print("----睡觉----")
  9. def run(self):
  10. print("----跑----")
  11.  
  12. class Dog(Animal):
  13. def bark(self):
  14. print("-----汪汪叫---")
  15.  
  16. class Xiaotq(Dog):
  17. def fly(self):
  18. print("---飞----")
  19.  
  20. xiaotianquan = Xiaotq()
  21. xiaotianquan.fly()
  22. xiaotianquan.bark()
  23. xiaotianquan.eat()
  1. ---飞----
  2. -----汪汪叫---
  3. ----吃----

    

3.重写

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

    

  1. class Animal:
  2. '''定义一个动物类'''
  3. def eat(self):
  4. print("----吃----")
  5. def drink(self):
  6. print("----喝----")
  7. def sleep(self):
  8. print("----睡觉----")
  9. def run(self):
  10. print("----跑----")
  11.  
  12. class Dog(Animal):
  13. def bark(self):
  14. print("-----汪汪叫---")
  15.  
  16. class Xiaotq(Dog):
  17. def fly(self):
  18. print("---飞----")
  19. def bark(self): #定义和父类方法同名的方法,就是重写
  20. print("---重写叫---")
  21.  
  22. xiaotianquan = Xiaotq()
  23. xiaotianquan.fly()
  24. xiaotianquan.bark()
  25. xiaotianquan.eat()
  1. ---飞----
  2. ---重写叫---
  3. ----吃----

4.调用被重写的方法

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

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

  1. class Animal:
  2. '''定义一个动物类'''
  3. def eat(self):
  4. print("----吃----")
  5. def drink(self):
  6. print("----喝----")
  7. def sleep(self):
  8. print("----睡觉----")
  9. def run(self):
  10. print("----跑----")
  11.  
  12. class Dog(Animal):
  13. def bark(self):
  14. print("-----汪汪叫---")
  15.  
  16. class Xiaotq(Dog):
  17. def fly(self):
  18. print("---飞----")
  19. def bark(self):
  20. print("---重写叫---")
  21.  
  22. #第一种调用被重写的父类的方法
  23. Dog.bark(self)
  24.  
  25. xiaotianquan = Xiaotq()
  26. xiaotianquan.fly()
  27. xiaotianquan.bark()
  28. xiaotianquan.eat()
  1. ---飞----
  2. ---重写叫---
  3. -----汪汪叫---
  4. ----吃----

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

  1. class Animal:
  2. '''定义一个动物类'''
  3. def eat(self):
  4. print("----吃----")
  5. def drink(self):
  6. print("----喝----")
  7. def sleep(self):
  8. print("----睡觉----")
  9. def run(self):
  10. print("----跑----")
  11.  
  12. class Dog(Animal):
  13. def bark(self):
  14. print("-----汪汪叫---")
  15.  
  16. class Xiaotq(Dog):
  17. def fly(self):
  18. print("---飞----")
  19. def bark(self):
  20. print("---重写叫---")
  21.  
  22. #第一种调用被重写的父类的方法
  23. # Dog.bark(self)
  24.  
  25. #第2种
  26. super().bark() #super()高级的,上级的
  27.  
  28. xiaotianquan = Xiaotq()
  29. xiaotianquan.fly()
  30. xiaotianquan.bark()
  31. xiaotianquan.eat()

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

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

  1)版本1:

  1. class A:
  2. def __init__(self):
  3. self.num1 = 100
  4. self.__num2 = 222
  5.  
  6. def test1(self):
  7. print("---test1---")
  8.  
  9. def __test2(self):
  10. print("--test2---")
  11.  
  12. class B(A):
  13. pass
  14.  
  15. b = B()
  16. b.test1()
  17. b.__test2() #私有方法不会被继承
  18.  
  19. print(b.num1)
  20. print(b.__num2) #私有属性不会被继承
  1. ---test1---
  2. Traceback (most recent call last):
  3. File "09-私有在继承中的表现.py", line 17, in <module>
  4. b.__test2() #私有方法不会被继承
  5. AttributeError: 'B' object has no attribute '__test2'

  2)版本2:

  1. class A:
  2. def __init__(self):
  3. self.num1 = 100
  4. self.__num2 = 222
  5.  
  6. def test1(self):
  7. print("---test1---")
  8.  
  9. def __test2(self):
  10. print("--test2---")
  11.  
  12. class B(A):
  13. pass
  14.  
  15. b = B()
  16. b.test1()
  17. #b.__test2() #私有方法不会被继承
  18.  
  19. print(b.num1)
  20. #print(b.__num2) #私有属性不会被继承
  1. ---test1---
  2. 100

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

  1. class A:
  2. def __init__(self):
  3. self.num1 = 100
  4. self.__num2 = 222
  5.  
  6. def test1(self):
  7. print("---test1---")
  8.  
  9. def __test2(self):
  10. print("--test2---")
  11.  
  12. def test3(self):
  13. self.__test2()
  14. print(self.__num2)
  15.  
  16. class B(A):
  17. pass
  18.  
  19. b = B()
  20. b.test1()
  21. #b.__test2() #私有方法不会被继承
  22.  
  23. print(b.num1)
  24. #print(b.__num2) #私有属性不会被继承
  25.  
  26. b.test3() #公有方法调用私有属性方法
  1. ---test1---
  2. 100
  3. --test2---
  4. 222

  4)版本4:

  1. class A:
  2. def __init__(self):
  3. self.num1 = 100
  4. self.__num2 = 222
  5.  
  6. def test1(self):
  7. print("---test1---")
  8.  
  9. def __test2(self):
  10. print("--test2---")
  11.  
  12. def test3(self):
  13. self.__test2()
  14. print(self.__num2)
  15.  
  16. class B(A):
  17. def test4(self):
  18. self.__test2()
  19. print(self.__num2)
  20.  
  21. b = B()
  22. b.test1()
  23. #b.__test2() #私有方法不会被继承
  24.  
  25. print(b.num1)
  26. #print(b.__num2) #私有属性不会被继承
  27.  
  28. b.test3()
  29. b.test4()
  1. ---test1---
  2. 100
  3. --test2---
  4. 222
  5. Traceback (most recent call last):
  6. File "09-私有在继承中的表现.py", line 29, in <module>
  7. b.test4()
  8. File "09-私有在继承中的表现.py", line 18, in test4
  9. self.__test2()
  10. AttributeError: 'B' object has no attribute '_B__test2'

    

6.多继承

经典类

  1. class Dog:
  2. pass

新式类   object是所有类的父类

  1. class Dog(object):
  2. pass

  

多继承

  1. class Base(object):
  2. def base(self):
  3. print("----this is base-")
  4.  
  5. class A(Base):
  6. def test1(self):
  7. print("---this is A---")
  8.  
  9. class B(Base):
  10. def test2(self):
  11. print("----this is B---")
  12.  
  13. class C(A,B):
  14. pass
  15.  
  16. c1 = C()
  17. c1.test1()
  18. c1.test2()
  19. c1.base() 
  1. ---this is A---
  2. ----this is B---
  3. ----this is base-

    

7.多继承注意点

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

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

  1. class Base(object):
  2. def test(self):
  3. print("----this is base-")
  4.  
  5. class A(Base):
  6. def test(self):
  7. print("---this is A---")
  8.  
  9. class B(Base):
  10. def test(self):
  11. print("----this is B---")
  12.  
  13. class C(A,B):
  14. def test(self):
  15. print("----this is C---")
  16.  
  17. c1 = C()
  18. c1.test()
  1. ----this is C---

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

  1. class Base(object):
  2. def test(self):
  3. print("----this is base-")
  4.  
  5. class A(Base):
  6. def test(self):
  7. print("---this is A---")
  8.  
  9. class B(Base):
  10. def test(self):
  11. print("----this is B---")
  12.  
  13. class C(A,B):
  14. pass
  15. # def test(self):
  16. # print("----this is C---")
  17.  
  18. c1 = C()
  19. c1.test()
  1. ---this is A---

  3)版本3:调用顺序

  1. class Base(object):
  2. def test(self):
  3. print("----this is base-")
  4.  
  5. class A(Base):
  6. def test(self):
  7. print("---this is A---")
  8.  
  9. class B(Base):
  10. def test(self):
  11. print("----this is B---")
  12.  
  13. class C(A,B):
  14. pass
  15. # def test(self):
  16. # print("----this is C---")
  17.  
  18. c1 = C()
  19. c1.test()
  20. 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. webstorm中导入git项目

    1.打开webStrom 配置git File–setting

  2. How Flask Routing Works

    @How Flask Routing Works The entire idea of Flask (and the underlying Werkzeug library) is to map UR ...

  3. BZOJ4870:[SHOI2017]组合数问题(组合数学,矩阵乘法)

    Description Input 第一行有四个整数 n, p, k, r,所有整数含义见问题描述. 1 ≤ n ≤ 10^9, 0 ≤ r < k ≤ 50, 2 ≤ p ≤ 2^30 − 1 ...

  4. concatenate函数

    numpy.concatenate((a1, a2, ...), axis=0) Join a sequence of arrays along an existing axis.(按轴axis连接a ...

  5. ThinkPHP5入门(三)----模型篇

    一.操作数据库 1.数据库连接配置 数据库默认的相关配置在项目的application\database.php中已经定义好. 只需要在模块的数据库配置文件中配置好当前模块需要连接的数据库的配置参数即 ...

  6. Java中23种设计模式(附代码样例)

    一.设计模式分类总体来说设计模式分为三大类:创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式.结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥接模式.组 ...

  7. staticmethod classmethod

    1. 静态方法 @staticmethod 只是名义上归类管,实际上静态方法里访问不了类或者实例中的任何属性 2. 类方法 @classmethod 只能访问类变量,不能访问实例变量 3.属性方法 @ ...

  8. 转:介绍几个著名的实用的Java反编译工具,提供下载

    from :http://www.glorze.com/219.html 反编译 众所周知,我们将源代码进行编译,生成可执行的程序或者容器发布包,这个将代码转换的过程就是编译的过程,而反编译就是将这些 ...

  9. php无限极分类处理

    /** * 无限极分类处理(通过递归方式实现) * @param $section 原始数据Array * @param $html 界面显示前缀,比如 |- * @param $spear 分级中所 ...

  10. BZOJ1969: [Ahoi2005]LANE 航线规划(LCT)

    Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 587  Solved: 259[Submit][Status][Discuss] Description ...