封装
继承
# class Animal:
# pass
#
# class xxx:
# pass
#
# class Dog(Animal,xxx):
# pass
#
#
# d = Dog()
# print(d.__class__)
# print(Dog.__class__)
#
#
# print(Dog.__bases__)
# print(Animal.__bases__)
# # object
# ---------------------继承-资源-----------------------------------------------
#
# class Animal:
# # 属性 和方法
# # 设置不同权限的属性和方法,继承当中,进行测试
# # 在子类当中,能否访问到这些资源
#
# a = 1
# _b = 2
# __c = 3
# def t1(self):
# print("t1")
#
# def _t2(self):
# print("t2")
#
# def __t3(self):
# print("t3")
#
# def __init__(self):
# print("init, Animal")
#
# class Person(Animal):
#
# def test(self):
# print(id(self.a))
# print(self.a)
# print(self._b)
# # print(self.__c)
#
# self.t1()
# self._t2()
# # self.__t3()
#
# p = Person()
# p.test()
#
# print(id(Animal.a))
# p.test()
class B:
age = 10
class A(B):
pass
print(A.age)
A.age = 9
print(B.age)
print(A.age)
print(A.__dict__)
print(B.__dict__)
# class C:
# age = "c"
#
# class B(C):
# age = "b"
#
# class A(B):
# age = "a"
# A -> B -> C
# class E:
# age = "e"
#
# class D:
# age = "d"
#
# class C(E):
# age = "c"
#
# class B(D):
# age = "b"
#
# class A(B, C):
# age = "a"
#
# print(A.age)
# A -> B -> D -> C -> E
class D:
age = "d"
class C(D):
age = "c"
class B(D):
age = "b"
class A(B, C):
age = "a"
print(A.age)
print (inspect.getmro(A))
# ---------------------继承-资源的访问顺序c3-----------------------------
# C3算法
# 真正步骤
# 两个公式
# L(object) = [object]
# L(子类(父类1, 父类2)) = [子类] + merge(L(父类1), L(父类2) , [父类1, 父类2])
# 注意
# + 代表合并列表
# merge算法
# 1. 第一个列表的第一个元素
# 是后续列表的第一个元素
# 或者
# 后续列表中没有再次出现
# 2. 如果不符合,则跳过此元素,查找下一个列表的第一个元素,重复1的判断规则
# 3. 如果最终无法把所有元素归并到解析列表, 则报错
# 类似拓扑排序, 但并不是! 切记
# 具体算法步骤
# 选择一个入度为0的顶点并输出之;
# 从网中删除此顶点及所有出边。
class D:
pass
# L(D(object)) = [D] + merge(L(object), [object])
# = [D] + merge([object],[object])
# = [D, object] + merge([], [])
# = [D, object]
class B(D):
pass
# L(B(D)) = [B] + merge(L(D), [D])
# = [B] + merge([D, object],[D])
# = [B, D] + merge([object], [])
# = [B, D, object] + merge([], [])
# = [B, D, object]
class C(D):
pass
class A(B, C):
pass
# L(A) = [A] + merge(L(B), L(C),[B, C])
# L(A) = [A] + merge([B, D, object], [C, D, object],[B, C])
# L(A) = [A, B] + merge([D, object], [C, D, object], [C])
# L(A) = [A, B, C] + merge([D, object], [D,object])
# L(A) = [A, B, C, D] + merge([object], [object])
# L(A) = [A, B, C, D, object] + merge([], [])
# L(A) = [A, B, C, D, object]
import inspect
print(inspect.getmro(A))
# ---------------------继承-资源的访问顺序c3-算法识别问题-----------------------------
class D():
pass
class B(D):
pass
class C(B):
pass
class A(B, C):
pass
import inspect
print(inspect.getmro(A))
##错的
# -------------------------继承-资源的覆盖----------------------------
class D():
age = "d"
class C(D):
age = "c"
def test(self):
print("C")
class B(D):
age = "b"
def test(self):
print("B")
class A(B, C):
# age = "a"
pass
print(A.age)
print(A().test())
print(A.mro())
import inspect
print(inspect.getmro(A))
# # ------------------------------------继承-资源的累加------------------------------------
#
# class B:
# a = 1
# def __init__(self):
# self.b = 2
#
# def t1(self):
# print("t1")
#
# @classmethod
# def t2(self):
# print("t2")
#
# @staticmethod
# def t3():
# print("t3")
#
# class A(B):
# c = 1
# def __init__(self):
# self.e = 5
# def tt1(self):
# print("t1")
#
# @classmethod
# def tt2(self):
# print("t2")
#
# @staticmethod
# def tt3():
# print("t3")
#
# pass
#
# a_obj = A()
# print(A.a)
# print(a_obj.b)
# a_obj.t1()
# A.t2()
# A.t3()
#
# print(A.c)
# a_obj.tt1()
# A.tt2()
# A.tt3()
# ------------------------------------继承-资源的累加2------------------------------------
class B:
a = 1
def __init__(self):
self.b = 2
def t1(self):
print("t1")
@classmethod
def t2(self):
print("t2")
@staticmethod
def t3():
print("t3")
class A(B):
c = 1
def __init__(self):
B.__init__(self)
self.e = 5
def tt1(self):
print("t1")
@classmethod
def tt2(self):
print("t2")
@staticmethod
def tt3():
print("t3")
pass
a_obj = A()
print(A.a)
print(a_obj.b)
a_obj.t1()
A.t2()
A.t3()
print(A.c)
a_obj.tt1()
A.tt2()
A.tt3()
class D:
def __init__(self):
print("d")
class B(D):
def __init__(self):
D.__init__(self)
print("b")
class C(D):
def __init__(self):
D.__init__(self)
print("c")
class A(B, C):
def __init__(self):
D.__init__(self)
C.__init__(self)
print("a")
# B()
# C()
A()
class Animal():
def jiao(self):
pass
class Dog(Animal):
def jiao(self):
print("汪汪汪")
class Cat(Animal):
def jiao(self):
print("喵喵喵")
def test(obj):
obj.jiao()
d = Dog()
c = Cat()
test(c)
import abc
class Animal(metaclass=abc.ABCMeta):
@abc.abstractmethod
def jiao(self):
pass
class Dog(Animal):
def jiao(self):
print("汪汪汪")
class Cat(Animal):
def jiao(self):
print("喵喵喵")
def test(obj):
obj.jiao()
# d = Dog()
# c = Cat()
# test(c)
d = Dog()
d.jiao()