day 4 继承
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 继承的更多相关文章
- javaScript的原型继承与多态性
1.prototype 我们可以简单的把prototype看做是一个模版,新创建的自定义对象都是这个模版(prototype)的一个拷贝 (实际上不是拷贝而是链接,只不过这种链接是不可见,给人们的感觉 ...
- JavaScript的继承实现方式
1.使用call或apply方法,将父对象的构造函数绑定在子对象上 function A(){ this.name = 'json'; } function B(){ A.call(this); } ...
- javascript中的继承与深度拷贝
前言 本篇适合前端新人,下面开始...... 对于前端新手来说(比如博主),每当对js的对象做操作时,都是一种痛苦,原因就是在于对象的赋值是引用的传递,并非值的传递,虽然看上去后者赋值给了前者,他们就 ...
- 谈谈一些有趣的CSS题目(四)-- 从倒影说起,谈谈 CSS 继承 inherit
开本系列,讨论一些有趣的 CSS 题目,抛开实用性而言,一些题目为了拓宽一下解决问题的思路,此外,涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题中有你感觉 ...
- JS继承类相关试题
题目一: //有关于原型继承的代码如下:function Person(name) { this.name = name;}Person.prototype = { getName : f ...
- JS继承之寄生类继承
原型式继承 其原理就是借助原型,可以基于已有的对象创建新对象.节省了创建自定义类型这一步(虽然觉得这样没什么意义). 模型 function object(o){ function W(){ } W. ...
- JS继承之借用构造函数继承和组合继承
根据少一点套路,多一点真诚这个原则,继续学习. 借用构造函数继承 在解决原型中包含引用类型值所带来问题的过程中,开发人员开始使用一种叫做借用构造函数(constructor stealing)的技术( ...
- JS继承之原型继承
许多OO语言都支持两种继承方式:接口继承和实现继承.接口继承只继承方法签名,而实现继承则继承实际的方法.如前所述,由于函数没有签名,在ECMAScript中无法实现接口继承.ECMAScript只支 ...
- 深入浅出JavaScript之原型链&继承
Javascript语言的继承机制,它没有"子类"和"父类"的概念,也没有"类"(class)和"实例"(instanc ...
- 如果你也会C#,那不妨了解下F#(7):面向对象编程之继承、接口和泛型
前言 面向对象三大基本特性:封装.继承.多态.上一篇中介绍了类的定义,下面就了解下F#中继承和多态的使用吧.
随机推荐
- webstorm中导入git项目
1.打开webStrom 配置git File–setting
- How Flask Routing Works
@How Flask Routing Works The entire idea of Flask (and the underlying Werkzeug library) is to map UR ...
- BZOJ4870:[SHOI2017]组合数问题(组合数学,矩阵乘法)
Description Input 第一行有四个整数 n, p, k, r,所有整数含义见问题描述. 1 ≤ n ≤ 10^9, 0 ≤ r < k ≤ 50, 2 ≤ p ≤ 2^30 − 1 ...
- concatenate函数
numpy.concatenate((a1, a2, ...), axis=0) Join a sequence of arrays along an existing axis.(按轴axis连接a ...
- ThinkPHP5入门(三)----模型篇
一.操作数据库 1.数据库连接配置 数据库默认的相关配置在项目的application\database.php中已经定义好. 只需要在模块的数据库配置文件中配置好当前模块需要连接的数据库的配置参数即 ...
- Java中23种设计模式(附代码样例)
一.设计模式分类总体来说设计模式分为三大类:创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式.结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥接模式.组 ...
- staticmethod classmethod
1. 静态方法 @staticmethod 只是名义上归类管,实际上静态方法里访问不了类或者实例中的任何属性 2. 类方法 @classmethod 只能访问类变量,不能访问实例变量 3.属性方法 @ ...
- 转:介绍几个著名的实用的Java反编译工具,提供下载
from :http://www.glorze.com/219.html 反编译 众所周知,我们将源代码进行编译,生成可执行的程序或者容器发布包,这个将代码转换的过程就是编译的过程,而反编译就是将这些 ...
- php无限极分类处理
/** * 无限极分类处理(通过递归方式实现) * @param $section 原始数据Array * @param $html 界面显示前缀,比如 |- * @param $spear 分级中所 ...
- BZOJ1969: [Ahoi2005]LANE 航线规划(LCT)
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 587 Solved: 259[Submit][Status][Discuss] Description ...