-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobject_oriented.py
More file actions
102 lines (79 loc) · 2.53 KB
/
object_oriented.py
File metadata and controls
102 lines (79 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# coding = utf-8
class Player(object):
def __init__(self, name, points):
self.name = name
self.points = points
def print_points(self):
#封装,即类在定义时指定访问内部数据的方式,这种方式通过类中定义的函数来实现
#封装数据的函数是和类本身关联在一起的,称之为类的方法
print('{}: {}'.format(self.name, self.points))
def get_points(self):
#封装后,在类中添加新的方法
if self.points >= 80:
print("Excellent!")
elif self.points >= 70:
print("good!")
else:
print("Come on!")
def do(self):
print("Player is shooting...")
class AllStar(Player):
#继承,Player的子类,具有父类Player的全部方法
def do(self):
#
print("Covered...")
def run(actor):
actor.print_points()
actor.print_points()
Kobe = Player('Kobe Bryant', 90)
print(Kobe)
print(Kobe.points)
print(Kobe.print_points())
print("-"*35)
print(Kobe.get_points())
Curry = AllStar('Kobe Bryant', 90)
Curry.do()
Kobe.do()
print('-'*35)
print(isinstance(Curry, AllStar))
print(isinstance(Curry, Player)) # 继承
print("-"*35)
#多态,新增任何一个父类的子类,不必对run()做任何修改,任何依赖Player作为参数的函数或者方法都可以不加任何修改即可正常运行,此为多态
run(Player("Joe", 70))
run(Curry)
run(AllStar("John", 100))
## MVC
class Model():
services = {
'email':{'number':1000, 'price':2},
'sms':{'number':1000, 'price':10},
'voice':{'number':1000, 'price':15}
}
class View():
def list_services(self, services):
for svc in services:
print(svc, '')
def list_price(self,services):
for svc in services:
print('For', Model.services[svc]['number'],
svc, "Message you pay $",
Model.services[svc]['price']
)
class Controller():
def __init__(self):
self.model = Model()
self.view = View()
def get_services(self):
services = self.model.services.keys()
return self.view.list_services(services)
def get_pricing(self):
services = self.model.services.keys()
return self.view.list_price(services)
class Client():
controller = Controller()
print("service provided:")
controller.get_pricing()
print("service for Services:")
controller.get_services()
if __name__ == "__main__":
client = Client()