每周学点土壤知识 2018-04-14
class BaseClass: num_base_calls =def call_me(self): print("Calling method on Based Class") self.num_base_calls += 1 class LeftSubclass(BaseClass): num_left_calls =def call_me(self): BaseClass.call_me(self) print("Calling method on Left Subclass") self.num_left_calls += 1 class RightSubclass(BaseClass): num_right_calls =def call_me(self): BaseClass.call_me(self) print("Calling method on Right Subclass") self.num_right_calls += 1 class Subclass(LeftSubclass, RightSubclass): num_sub_calls =def call_me(self): LeftSubclass.call_me(self) RightSubclass.call_me(self) print("Calling method on Subclass") self.num_sub_calls += 1 s = Subclass() s.call_me() print(s.num_sub_calls, s.num_left_calls, s.num_right_calls, s.num_base_calls)
class BaseClass: num_base_calls =def call_me(self): print("Calling method on Based Class") self.num_base_calls += 1 class LeftSubclass(BaseClass): num_left_calls =def call_me(self): # BaseClass.call_me(self) super().call_me() print("Calling method on Left Subclass") self.num_left_calls += 1 class RightSubclass(BaseClass): num_right_calls =def call_me(self): # BaseClass.call_me(self) super().call_me() print("Calling method on Right Subclass") self.num_right_calls += 1 class Subclass(LeftSubclass, RightSubclass): num_sub_calls =def call_me(self): # LeftSubclass.call_me(self) # RightSubclass.call_me(self) super().call_me() print("Calling method on Subclass") self.num_sub_calls += 1 s = Subclass() s.call_me() print(s.num_sub_calls, s.num_left_calls, s.num_right_calls, s.num_base_calls)
参考:本文参考学习《Python3 Object Oriented Programming》,根据自己理解改编,Dusty Phillips 著