1 class A(object): 2 x = 1 3 4 class B(A): 5 pass 6 7 class C(A): 8 pass 9 10 # 通過父類修改類屬性,子類繼承的類屬性也改變 11 A.x = 3 12 print(A.x, B.x, C.x) 13 # 通過子類修改繼承... ...
1 class A(object): 2 x = 1 3 4 class B(A): 5 pass 6 7 class C(A): 8 pass 9 10 # 通過父類修改類屬性,子類繼承的類屬性也改變 11 A.x = 3 12 print(A.x, B.x, C.x) 13 # 通過子類修改繼承自父類的類屬性,只會改變該子類繼承的類屬性 14 B.x = 2 15 print(A.x, B.x, C.x) 16 # 子類先修改繼承的類屬性,父類再修改類屬性,將不會再對已經修改過的子類類屬性做出改變 17 B.x = 4 18 A.x = 1 19 print(A.x, B.x, C.x)