# encoding: utf-8
import abc
# ************************************************继承************************************************
# 1 继承的三种形态:
# 单继承
# 无重叠的多继承(资源访问顺序按遵循单调性)
# 有重叠的多继承
# 2 ************************************************
# super
# 沿着MRO链条,找到下一级方法,调用对应的方法
class D:
def __init__(self):
print("d")
class C(D):
def __init__(self):
super(C, self).__init__()
print("c")
class B(D):
def __init__(self):
super(B, self).__init__()
print("b")
class A(B, C):
def __init__(self):
super(A, self).__init__()
print("a")
A()
# 3 ************************************************
# 抽象类
class Animal(metaclass=abc.ABCMeta):
@abc.abstractmethod
def call(self):
pass
@abc.abstractclassmethod
def test(cls):
pass
class Dog(Animal):
def call(self):
print("wang wang wang")
@classmethod
def test(cls):
print(cls.__dict__)
class Cat(Animal):
def call(self):
print("miao miao miao")
@classmethod
def test(cls):
print(cls.__dict__)
d = Dog()
c = Cat()
c.call()
d.call()
python | 面向对象 | 继承
最新推荐文章于 2024-10-07 21:17:34 发布