面向对象继承与派生

1、什么是继承?

继承是一种新建类的方式,新建的类称为子类,被继承的类称为父类

继承的特性是:子类会遗传父类的属性

强调:继承是类与类之间的关系

2、为什么要用继承

继承的好处就是可以减少代码的冗余

3、如何用继承

在python中支持一个类同时继承多个父类

        在python3中如果一个类没有继承任何类,那默认继承object;

        在python2中如果一个类没有继承任何类,不会继承object类

新兴类

       但凡继承了object的类以及该类的子类,都是新式类;

       没有继承object的类以及该类的子类,都是经典类

(在python3中都是新式类,只有在python中才区别新式类与经典类)

class Parent1(object):
pass

class Parent2(object):
pass
class Sub1(Parent1,Parent2):
pass
print(Sub1.__bases__) #(<class '__main__.Parent1'>, <class '__main__.Parent2'>)
print(Parent1.__bases__) #(<class 'object'>,)
print(Parent2.__bases__) #(<class 'object'>,)

 

继承应用

# class OldboyPeople:
# school = 'oldboy'
#
# class OldboyStudent:
# school='oldboy'
# def __init__(self,name,age,sex):
# self.name=name
# self .age= age
# self .sex=sex
# def choose_course(self):
# print('%s is choosing course'%(self.name))
#
#
# class OldboyTeacher:
# school= 'Oldboy'
# def __init__(self,name,age,sex,level):
# self.name= name
# self .age= age
# self.sex= sex
# self .level= level
#
# def score(self):
# print('%s is scoring'%self .name)
#
# stu1= OldboyStudent('haoge',34,'male')
# tea1=OldboyTeacher('egon',44,'gg',20)
#
# stu1.choose_course()
# tea1.score()
#
# print(stu1.school) #查找属性的顺序:对象自己》对象的类》父类。。

 

 

4、派生

(1)#派生:子类中新定义的属性,子类在使用时始终以自己的为准
class OldboyPeople:
school = 'oldboy' #子类中相同的属性
def __init__(self,name,age,sex):
self.name=name
self .age= age
self .sex=sex
class OldboyStudent(OldboyPeople):
school='oldboy'

def choose_course(self):
print('%s is choosing course'%(self.name))


class OldboyTeacher(OldboyPeople):
school= 'Oldboy'
def __init__(self,name,age,sex,level):
OldboyPeople.__init__(self ,name,age,sex) #调用父集的属性
self .level= level

def score(self,stu_obj,num):
print('%s is scoring'%self .name)


stu1 = OldboyStudent('haoge', 34, 'male')
tea1=OldboyTeacher('egon',44,'gg',20)

tea1.score(stu1,99)

 

(2)在子类派生的新方法中重用父类功能的两种方式

# 方式一:与继承无关
#指名道姓法,直接用:类名.函数名
# class OldboyPeople:
# school = 'oldboy'
#
# def __init__(self, name, age, sex):
# self.name = name
# self.age = age
# self.sex = sex
#
# class OldboyStudent(OldboyPeople):
# def __init__(self,name,age,sex,stu_id):
# OldboyPeople.__init__(self,name,age,sex)
# self.stu_id=stu_id
#
# def choose_course(self):
# print('%s is choosing course' %self.name)
#
# stu1= OldboyStudent('猪哥',66,'mal',10)
# stu1.choose_course()
# print(stu1 .school)
# print(stu1 .name )


# 方式二:严格以继承属性查找关系
# super()会得到一个特殊的对象,该对象就是专门用来访问父类中的属性的(按照继承的关系)
# super().__init__(不用为self传值)
# 注意:
# super的完整用法是super(自己的类名,self),在python2中需要写完整,而python3中可以简写为super()
class OldboyPeople:
school = 'oldboy'

def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex

class OldboyStudent(OldboyPeople):
def __init__(self,name,age,sex,stu_id):
super().__init__(name,age,sex) #完整写法super( OldboyStudent,self).__init__(name,age,sex))
self.stu_id=stu_id

def choose_course(self):
print('%s is choosing course' %self.name)


stu1=OldboyStudent('猪哥',19,'male',1)
print(stu1.__dict__)

print(OldboyStudent.mro()) #根据c3算法获取继承顺序进行查找

例题:

class A:
def f1(self):
print('A.f1')

class B:
def f2(self):
super().f1() #重用父类功能A.f1(self)
print('B.f2')

class C(B,A):
pass

obj=C()
print(C.mro()) # 查找顺序C-》B->A->object
obj.f2()

 

posted @ 2018-06-27 15:28  朝朝哥  阅读(266)  评论(0编辑  收藏  举报