标签:调用 传参 people 创建 派生类 ase 关系 oldboy 派生
1继承介绍
‘‘‘
1. 什么是继承?
在程序中继承是一种新建子类的方式,新创建的类称之为子类\派生类,被继承的类称之为父类\基类\超类
继承描述的是一种遗传关系,子类可以重用父类的属性
2. 为何用继承?
减少类与类之间代码冗余的问题
3. 如何继承
先抽象再继承
‘‘‘
# 在python中继承的特点?单继承&多继承
# class Parent1(object):
# pass
#
# class Parent2:
# pass
#
# class Sub1(Parent1):
# pass
#
# class Sub2(Parent1,Parent2):
# pass
#
# # print(Sub1.__bases__)#继承了谁会输出
# # print(Sub2.__bases__)
#
# print(Parent1.__bases__)
# print(Parent2.__bases__)
# x=2
# class Foo:
# # x=1
# pass
# obj=Foo()
# # obj.x=3
# print(obj.x)
# python2与python3在继承上的区别
# 新式类:但凡继承object类的子类,以及该子类的子子类,...都称之为新式类
# 经典类:没有继承object类的子类,以及该子类的子子类,...都称之为经典类
# 只有在python2中才区分新式类与经典类
# 新式类与经典在属性查找上的区别???
2 基于继承解决类与类代码冗余问题
# 在子类派生出的新功能中如何重用父类的功能:
# 方式一: 指名道姓地访问某一个类中的函数,与继承无关
class OldboyPeople:
school = ‘Oldboy‘
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
# print(OldboyPeople.__init__)
class OldboyStudent(OldboyPeople):
# def __init__(self, name, age, gender):
# self.name = name
# self.age = age
# self.gender = gender
def choose_course(self):
print(‘%s is choosing course‘ %self.name)
class OldboyTeacher(OldboyPeople):
# tea, ‘egon‘, 18, ‘male‘, 10, 3000
def __init__(self, name, age, gender,level,salary):
# self.name = name
# self.age = age
# self.gender = gender
OldboyPeople.__init__(self, name, age, gender)
self.level=level
self.salary=salary
def score(self,stu,num):
stu.num=num
print(‘老师%s给学生%s打分%s‘ %(self.name,stu.name,num))
# stu=OldboyStudent(‘kevin‘,38,‘male‘) #__init___(stu1,‘kevin‘,38,‘male‘)
# print(stu.__dict__)
tea=OldboyTeacher(‘egon‘,18,‘male‘,10,3000) #__init___(tea,‘egon‘,18,‘male‘,10,3000)
print(tea.__dict__)
# print(stu.school)
# print(tea.school)
3
# 在单继承背景下,无论是新式类还是经典类属性查找顺序都一样
# 先obj->类->父类->...
# class Foo:
# def f1(self):
# print(‘Foo.f1‘)
#
# def f2(self):
# print(‘Foo.f2‘)
# self.f1() #obj.f1()
#
# class Bar(Foo):
# def f1(self):
# print(‘Bar.f1‘)
#
# obj=Bar()
# obj.f2()
# 在多继承背景下,如果一个子类继承了多个分支,但是多个分支没有汇聚到一个非object类,无论是新式类还是经典类属性查找顺序都一样:
# 会按照从左到右的顺序一个分支一个分支的查找下去
class E:
# xxx=‘E‘
pass
class F:
# xxx=‘F‘
pass
class B(E):
# xxx=‘B‘
pass
class C(F):
# xxx=‘C‘
pass
class D:
# xxx=‘D‘
pass
class A(B,C,D):
# xxx=‘A‘
pass
obj=A()
# obj.xxx=111
# print(obj.xxx)
print(A.mro())
# 在多继承背景下,如果一个子类继承了多个分支,但是多个分支最终汇聚到一个非object类(菱形继承问题)
# 新式类:广度优先查找:obj->A->B->E->C->F->D->G->object
# 经典类:深度优先查找:obj->A->B->E->G->C->F->D
# class G:
# xxx=‘G‘
#
# class E(G):
# xxx=‘E‘
# pass
#
# class F(G):
# xxx=‘F‘
# pass
#
# class B(E):
# xxx=‘B‘
# pass
#
# class C(F):
# xxx=‘C‘
# pass
#
# class D(G):
# xxx=‘D‘
# pass
#
# class A(B,C,D):
# xxx=‘A‘
# pass
#
#
# print(A.mro())
4 super方法
# 在子类派生出的新功能中如何重用父类的功能:
# 方式一: 指名道姓地访问某一个类中的函数,与继承无关
# 方式二: super(OldboyTeacher,self),在python3中super可以不传参数,调用该函数会得到一个特殊的对象,该对象是专门用来访问父类中属性,
# 强调:super会严格参照类的mro列表依次查找属性
# class OldboyPeople:
# school = ‘Oldboy‘
# def __init__(self, name, age, gender):
# self.name = name
# self.age = age
# self.gender = gender
#
# class OldboyTeacher(OldboyPeople):
# # tea, ‘egon‘, 18, ‘male‘, 10, 3000
# def __init__(self, name, age, gender,level,salary):
# OldboyPeople.__init__(self, name, age, gender)
# # super(OldboyTeacher,self).__init__(name, age, gender)
#
# self.level=level
# self.salary=salary
#
# def score(self,stu,num):
# stu.num=num
# print(‘老师%s给学生%s打分%s‘ %(self.name,stu.name,num))
#
# tea=OldboyTeacher(‘egon‘,18,‘male‘,10,3000) #__init___(tea,‘egon‘,18,‘male‘,10,3000)
# print(tea.__dict__)
# # print(stu.school)
#A没有继承B,
class A:
def test(self):
print(‘A.test‘)
super().test()
class B:
def test(self):
print(‘from B‘)
class C(A,B):
pass
c=C()
c.test()
print(C.mro())
标签:调用 传参 people 创建 派生类 ase 关系 oldboy 派生
原文地址:https://www.cnblogs.com/jutao/p/9863693.html