20、继承的应用、super、组合

Day 20

一、继承应用+super的基本用法

class People:
    school = '虹桥校区'

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


class Student(People):

    def choose(self):
        print('%s 选课成功' % self.name)


class Teacher(People):

    def __init__(self, name, age, gender, level):
    
    
        # 方式一: 指名道姓地引用某一个类的函数,与继承无关
        # People.__init__(self, name, age, gender)
        
        
		# 方式二: super()返回一个特殊的对象,该对象会参考发起
		属性查找的那一个类的mro列表,去当前类的父类中找属性,严
		格依赖继承
        super(Teacher, self).__init__(name, age, gender)
        self.level = level

    def score(self):
        print('%s正在为学生打分' % self.name)


stu1 = Student('jack', 18, 'male')
stu2 = Student('tom', 38, 'male')
stu3 = Student('lili', 28, 'female')

tea1 = Teacher('egon', 18, 'male', 10)
tea2 = Teacher('lxx', 38, 'male', 3)


print(stu1.__dict__)
print(stu2.__dict__)
print(stu3.__dict__)
print(tea1.__dict__)
print(tea2.__dict__)
======================输出结果=========================
{'name': 'jack', 'age': 18, 'gender': 'male'}
{'name': 'tom', 'age': 38, 'gender': 'male'}
{'name': 'lili', 'age': 28, 'gender': 'female'}
{'name': 'egon', 'age': 18, 'gender': 'male', 'level': 10}
{'name': 'lxx', 'age': 38, 'gender': 'male', 'level': 3}
面向对象代码,写着烦,用着爽!!!

二、继承实现原理

python会根据C3算法给你算出mro列表出来

如果是单继承,一层一层的向上找。

class C:
    x = 333

class B(C):
    x = 222

class A(B):
    # x = 111
    pass

print(B.mro())
# print(A.mro())

obj = A()
# print(obj.x)

菱形继承/死亡钻石:一个子类继承的多条分支最终汇聚一个非object的类上
经典类:深度优先
新式类:广度优先

class G: # 在python2中,未继承object的类及其子类,都是经典类
    def test(self):
        print('from G')

class E(G):
    def test(self):
        print('from E')

class F(G):
    def test(self):
        print('from F')

class B(E):
    # def test(self):
    #     print('from B')
    pass

class C(F):
    def test(self):
        print('from C')

class D(G):
    def test(self):
        print('from D')

class A(B,C,D):
    # def test(self):
    #     print('from A')
    pass

obj = A()  # A->B->E->C->F->D->G->object
# print(A.mro())

obj.test()

四、多继承的代码规范

以mixin来表示,代表着为这个类添加的功能

# 继承表达式一个is-a的关系
class Vehicle:
    pass

class FlyableMixin:
    def fly(self):
        print('flying')

class CivilAircraft(FlyableMixin,Vehicle):
    pass

class Helicopter(FlyableMixin,Vehicle):
    pass

class Car(Vehicle):
    pass

五、组合

在一个类中以另外一个类的对象作为数据属性,称为类的组合。组合与继承都是用来解决代码的重用性问题。不同的是:继承是一种“是”的关系,比如老师是人、学生是人,当类之间有很多相同的之处,应该使用继承;而组合则是一种“有”的关系,比如老师有生日,老师有多门课程,当类之间有显著不同,并且较小的类是较大的类所需要的组件时,应该使用组合

简述--组合: 一个对象的属性值是指向另外一个类的对象

class People:
    school = "虹桥校区"

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

class Student(People):
    def choose(self):
        print("%s 选课成功" %self.name)

class Teacher(People):
    #            空对象,'egon',18,"male",10
    def __init__(self,name,age,gender,level):
        People.__init__(self,name,age,gender)

        self.level = level

    def score(self):
        print("%s 正在为学生打分" %self.name)

class Course:
    def __init__(self,name,price,period):
        self.name = name
        self.price = price
        self.period =period

    def tell(self):
        print('课程信息<%s:%s:%s>' %(self.name,self.price,self.period))

python = Course("python全栈开发",19800,"6mons")
linux = Course("linux",19000,"5mons")



stu1 = Student("jack",18,"male")
stu2 = Student("tom",19,"male")
stu3 = Student('lili',29,"female")


tea1 = Teacher('egon',18,"male",10)  # 空对象,'egon',18,"male",10
tea2 = Teacher('lxx',38,"male",3)


stu1.courses = []
stu1.courses.append(python)
stu1.courses.append(linux)

print(stu1.courses)
for course_obj in stu1.courses:
    course_obj.tell()
posted @ 2021-01-12 19:26  BaiM0  阅读(72)  评论(0编辑  收藏  举报