Hzangyao

导航

单和多继承属性查找顺序,super,多态,组合

单继承下的属性查找顺序

# class Foo:
#     def f1(self):
#         print('from Foo.f1')
#
#     def f2(self):
#         print('from Foo.f2')
#         self.f1()
#
#
# class Bar(Foo):
#     def f1(self):
#         print('from Bar.f1')
#
# # 属性查找的顺序:现在对象自己的名称空间中查找,在去产生对象的类中查找,在去继承的类中查找
#
# obj = Bar()
# obj.f2()

class Foo:
    def __f1(self):  # _Foo__f1()
        print('from Foo.f1')

    def f2(self):
        print('from Foo.f2')
        self.__f1()   # _Foo__f1()


class Bar(Foo):
    def __f1(self):  # _Bar__f1()
        print('from Bar.f1')

# 属性查找的顺序:现在对象自己的名称空间中查找,在去产生对象的类中查找,在去继承的类中查找

obj = Bar()
obj.f2()

多继承下的属性查找

# 经典类:深度优先查询
# 新式类:广度优先查询

class G(object):
    # def test(self):
    #     print('from G')
    pass


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

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

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

class H():
    # def test(self):
    #     print('from C')
    pass

class D(H):
    # def test(self):
    #     print('from D')
    pass

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


f1 = A()
f1.test()

super和mro列表

# class People():
#     school = 'SH'
#
#     def __init__(self, name, age, gender):
#         self.name = name
#         self.age = age
#         self.gender = gender
#
#
# class Student(People):
#     def __init__(self, name, age, gender, course=None):
#         if course is None:
#             course = []
#         # People.__init__(self, name, age, gender)  # 指名道姓的调用
#         # super(Student, self) 返回的是一个特殊的对象
#         # super(Student, self).__init__(name, age, gender)  # python2中的写法
#         super().__init__(name, age, gender)  # python3中的写法
#         self.course = course
#
#     def choose_course(self):
#         print('aaaa')
#
# stu = Student('tom', 19, 'male')
# print(stu.school)
# class Teacher(People):
#     def __init__(self, name, age, gender, level):
#         People.__init__(self, name, age, gender)
#         self.level = level
#
#     def score(self):
#         print('打分')

class A():
    def test(self):
        super().test()


class B:
    def test(self):
        print('from B')


class C(A, B):
    pass


c = A()
c.test()

多态与多态性

多态:同一种事物的多种形态
水:液态,固态,气态
动物:人,猪,狗,猫...

Animal类已经变成抽象类了,
抽象类的特点:只能被继承,不能被实例化
class Animal(metaclass=abc.ABCMeta):

    @abc.abstractmethod
    def speak(self):pass


class People(Animal):
    def jiao(self):
        pass

    def speak(self):
        pass


class Pig(Animal):
    def speak(self):
        pass


class Dog(Animal):
    def speak(self):
        pass
obj1 = People()
obj1.speak()

组合

# 组合:一个对象拥有一个属性,该属性的值是另外一个对象

继承:满足什么是什么的关系
组合:满足什么有什么的关系


class People():
    school = 'SH'

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


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

python=Course('python', '6mon', 30000)
linux=Course('linux', '5mon', 20000)
# print(python.name)
# print(python.period)
# print(python.price)

class Student(People):
    def __init__(self, name, age, gender,  course=None):
        if course is None:
            course = []
        # People.__init__(self, name, age, gender)  # 指名道姓的调用
        # super(Student, self) 返回的是一个特殊的对象
        # super(Student, self).__init__(name, age, gender)  # python2中的写法
        super().__init__(name, age, gender, )  # python3中的写法
        self.course = course


    def choose_course(self):
        print('aaaa')

stu = Student('tom', 19, 'male')
stu.course.append(python)
stu.course.append(linux)
for i in stu.course:
    print(i.name)

posted on 2022-02-16 20:47  Hzangyao  阅读(27)  评论(0编辑  收藏  举报