python-类与继承
类的继承
什么是继承?
继承是一种新建类的方式,新建的类称为子类,被继承的类称为父类。python中,父类、子类(派生类)只有在继承的时候才会产生。
继承的特性:子类会继承父类所有的属性。
为什么要用继承?
使用继承可以减少代码的冗余。
对象的继承
python中支持一个类同时继承多个父类(不推荐使用,当继承多个父类的时候,功能与功能之间会出现混乱。)
使用__bases__方法可以获取对象继承的类
class A:
def __init__(self,a):
self.a =a
class B:
def __init__(self,b):
self.b =b
class C(A,B):
pass
print(C.__bases__)
(<class '__main__.A'>, <class '__main__.B'>)
在python3中如果一个类没有继承任何类,则默认继承object类
在python2中如果一个类没有继承任何类,不会继承object类
对象查找属性的顺序
对象本身--->对象的类--->父类--->父类的父类,如果还没找到就会报错
class Foo:
def f1(self):
print('Foo.f1')
def f2(self): # self = b
print('Foo.f2')
self.f1() # b.f1()
class Bar(Foo):
def f1(self): # b.f1()
print('Bar.f1')
b = Bar()
b.f2() # Foo.f2
Foo.f2
Bar.f1
类的派生
派生:子类中添加新的属性得过程,这个过程就叫做派生;注意的是,在添加新的属性同时也会继承父类所有的东西。
派生方法一
类似于函数调用
class Animal:
def __init__(self,height,weight):
self.height = height
self.weight = weight
def jiao(self):
print('jiao')
class People(Animal):
def __init__(self,height,weight,name,age):
Animal.__init__(self,height,weight) #这个其实就是函数调用,不要继承也能实现
self.name = name
self.age = age
def read(self):
print('read')
派生方法二
super().__init__方法
class Animal:
def __init__(self,height,weight):
self.height = height
self.weight = weight
def jiao(self):
print('jiao')
class People(Animal):
def __init__(self,height,weight,name,age):
super().__init__(self,height,weight) # python3中里面不需要添加参数,但是python2 中需要添加super(People,self)
self.name = name
self.age = age
def read(self):
print('read')
类的组合
组合是用来解决类与类之间代码冗余的问题
class People:
def __init__(self,name,gender):
self.name = name
self.gender = gender
class Students(People):
def __init__(self,name,gender,id):
super().__init__(name,gender)
self.id = id
def choose_course(self,course):
print(f'{self.name}选择了{course.name}')
class Teachers(People):
def __init__(self,name,gender,level):
super().__init__(name,gender)
self.level = level
def score(self, student, course, score):
print(f'{self.name}給学生{student.name}课程{course.name}打分{score}')
class Course():
def __init__(self,name,price):
self.name =name
self.price = price
class Admin(People):
def create_course(self,name,price): # create_course = Course
course = Course(name,price)
print(f'{self.name}创建了课程{name}')
return course
# 实例化学生
rayn = Students('rayn','male','01')
rose = Students('rose','fmale','02')
# 实例化老师
nick = Teachers('nick','male',1)
tank = Teachers('tank','fmale',2)
#实例化管理员
egon = Admin('egon','male')
# 实例化课程
python = egon.create_course('python','20000') #egon.create_course == egon.Course
linux = egon.create_course('linux','18000')
# print(python.__dict__)
rayn.choose_course(python)
rose.choose_course(linux)
nick.score(rayn,python,100)
tank.score(rose,linux,1)
菱形继承问题
类的分类
新式类
继承了object的类以及该类的子类,都是新式类;python3中所有的类都是新式类。
经典类
没有继承object的类以及该类的子类,都是经典类,只有python2中才有经典类。
菱形继承问题
如果继承关系为菱形结构,即子类的父类最后继承了同一个类,那么属性的查找方式有两种:
- 经典类:深度优先
- 新式类:广度优先
我们也可以用print(类名.mro())来查看类的继承关系
类的多态与多态性
多态:
多态值的是一类事物有多种形态(一个抽象类有多个子类,因而多态的概念依赖于继承)
多态性:
多态性是指具有不同功能的函数,可以使用相同的函数名;这样就可以用一个函数名调用不同内容的函数。
python-类与继承的更多相关文章
- Python类的继承(进阶5)
转载请标明出处: http://www.cnblogs.com/why168888/p/6411918.html 本文出自:[Edwin博客园] Python类的继承(进阶5) 1. python中什 ...
- 孤荷凌寒自学python第二十二天python类的继承
孤荷凌寒自学python第二十二天python类的继承 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) python中定义的类可以继承自其它类,所谓继承的概念,我的理解 是,就是一个类B继承自 ...
- python 类定义 继承
0 前言 系统:win7 64bit IDE : python(x,y) 2.7.6.1 IDE集成的解释器:Python 2.7.6 (default, Nov 10 2013, 19:24:18) ...
- 记录Python类与继承的一个错误
今天在学python的类与继承的时候遇到一个错误,原来是自己在ctrl+c ctrl+v的时候漏了一个括号 class Car(): def __init__(self,make,year,mode ...
- Python 类的继承__init__() takes exactly 3 arguments (1 given)
类(class),可以继承基类以便形成具有自己独特属性的类,我们在面向对象的编程中,经常用到类及其继承,可以说没有什么不是类的,今天我们就来详细探讨一下在python中,类的继承是如何做的. 我们假设 ...
- day30 python类的继承,抽象类等
Python之路,Day17 = Python基础17-面向对象入门 继承 class Student(People): pass print(Student.__bases__) # 查看 Stud ...
- python类、继承
Python 是一种面向对象的编程语言.Python 中的几乎所有东西都是对象,拥有属性和方法.类(Class)类似对象构造函数,或者是用于创建对象的"蓝图". 一.python ...
- python类的继承和多态,获取对象信息
继承 类的继承机制使得子类可以继承父类中定义的方法,拥有父类的财产,比如有一个Animal的类作为父类,它有一个eat方法: class Animal(object): def __init__(se ...
- python类的继承、多继承及其常用魔术方法
继承 一个类可以派生出一个子类,这个子类可以使用父类的属性及方法,也可以在父类的基础上添加自己的独特属性或方法.属性和方法的继承的顺序是先从自己开始,找不到再去找父类,父类没有再找父类的父类,其尽头就 ...
- python类的继承
继承一个类 如果已经定义了Person类,需要定义新的Student和Teacher类时,可以直接从Person类继承: class Person(object): def __init__(self ...
随机推荐
- Codeforces 1C(外接圆与正多边形)
要点 各点肯定都在外接圆上,边越多越接近圆面积,所以要最小面积应当取可能的最少边数. 给三角形求外接圆半径公式:\(R=\frac{abc}{4S}\). 三个角度对应的圆心角取gcd即是要求的正多边 ...
- Avito Cool Challenge 2018-B. Farewell Party(思维)
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- bzoj3295: [Cqoi2011]动态逆序对 三维数点
为了便于考虑,把删除反序变为增加 于是就变成关于权值和位置和时间的三维数点 一波cdq一波树状数组教做人 (神TM需要longlong,80了一发) #include <bits/stdc++. ...
- 过流监测芯片ADS720/723
在电机应用领域经常需要用到过流监测和保护,allegro的ADS系列就可以很好实现.将芯片串接在电机之前,根据自己要保护的电流大小选择合适的量程,个根据自己ADC测量电压范围选择合适的灵敏度.这类芯片 ...
- SQL server 游标,随机数使用
SELECT * FROM [goods] DECLARE test_cursor CURSOR scroll FOR SELECT goods_no, [unitprice] FROM [goods ...
- SPRING代理模式
1.静态代理 主题对象:Student public interface Student { public String add(); } 目标对象:RealStudent public class ...
- android :fragmentation使用中遇到的 NullPointerException
背景:fragmentation(单ACTIVITY+多个fragments)+brvah( recyclerView多级自定义菜单功能) 目的:实现 菜单栏的点击,fragment 显示相应的内 ...
- VC中包含的头文件名不区分大小写
VC中包含的头文件名,不区分大小写如 #include "my.h" = #include "MY.H".
- Typedef 用法
typedef声明有助于创建平台无关类型,甚至能隐藏复杂和难以理解的语法. 不管怎样,使用 typedef 能为代码带来意想不到的好处,通过本文你可以学习用typedef避免缺欠,从而使代码更健壮. ...
- 读Linear Algebra -- Gilbert Strang
转眼间我的学士学位修读生涯已经快要到期了,重读线性代数,一是为了重新理解Algebra的的重要概念以祭奠大一刷过的计算题,二是为了将来的学术工作先打下一点点(薄弱的)基础.数学毫无疑问是指导着的科研方 ...