When Objects are Alike(Chapter 3 of Python 3 Object Oriented Programming)
时间:2010-09-01 来源:Ray Z
code
1 class BaseClass:
2 num_base_calls = 0
3
4 def call_me(self):
5 print("Calling method on Base Class")
6 self.num_base_calls += 1
7
8 class LeftSubclass(BaseClass):
9 num_left_calls = 0
10
11 def call_me(self):
12 BaseClass.call_me(self)
13 print("Calling method on Left Subclass")
14 self.num_left_calls += 1
15
16 class RightSubclass(BaseClass):
17 num_right_calls = 0
18
19 def call_me(self):
20 BaseClass.call_me(self)
21 print("Calling method on Right Subclass")
22 self.num_right_calls += 1
23
24 class Subclass(LeftSubclass, RightSubclass):
25 num_sub_calls = 0
26
27 def call_me(self):
28 LeftSubclass.call_me(self)
29 RightSubclass.call_me(self)
30 print("Calling method on Subclass")
31 self.num_sub_calls += 1
2 num_base_calls = 0
3
4 def call_me(self):
5 print("Calling method on Base Class")
6 self.num_base_calls += 1
7
8 class LeftSubclass(BaseClass):
9 num_left_calls = 0
10
11 def call_me(self):
12 BaseClass.call_me(self)
13 print("Calling method on Left Subclass")
14 self.num_left_calls += 1
15
16 class RightSubclass(BaseClass):
17 num_right_calls = 0
18
19 def call_me(self):
20 BaseClass.call_me(self)
21 print("Calling method on Right Subclass")
22 self.num_right_calls += 1
23
24 class Subclass(LeftSubclass, RightSubclass):
25 num_sub_calls = 0
26
27 def call_me(self):
28 LeftSubclass.call_me(self)
29 RightSubclass.call_me(self)
30 print("Calling method on Subclass")
31 self.num_sub_calls += 1
相关阅读 更多 +