-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoop3.py
69 lines (54 loc) · 1.73 KB
/
oop3.py
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
#!/usr/bin/python3
# oop3.py by Bill Weinman <http://bw.org/contact/>
# OOP/Polymorphism example in Python
# This is an exercise file from Python 3 Essential Training on lynda.com
# Copyright (c) 2010 The BearHeart Group, LLC
# -- VIEW --
class AnimalActions:
def bark(self): return self._doAction('bark')
def fur(self): return self._doAction('fur')
def quack(self): return self._doAction('quack')
def feathers(self): return self._doAction('feathers')
def _doAction(self, action):
if action in self.strings:
return self.strings[action]
else:
return 'The {} has no {}'.format(self.animalName(), action)
def animalName(self):
return self.__class__.__name__.lower()
# -- MODEL --
class Duck(AnimalActions):
strings = dict(
quack = "Quaaaaak!",
feathers = "The duck has gray and white feathers."
)
class Person(AnimalActions):
strings = dict(
bark = "The person says woof!",
fur = "The person puts on a fur coat.",
quack = "The person imitates a duck.",
feathers = "The person takes a feather from the ground and shows it."
)
class Dog(AnimalActions):
strings = dict(
bark = "Arf!",
fur = "The dog has white fur with black spots.",
)
# -- CONTROLLER --
def in_the_doghouse(dog):
print(dog.bark())
print(dog.fur())
def in_the_forest(duck):
print(duck.quack())
print(duck.feathers())
def main():
donald = Duck()
john = Person()
fido = Dog()
print("-- In the forest:")
for o in ( donald, john, fido ):
in_the_forest(o)
print("-- In the doghouse:")
for o in ( donald, john, fido ):
in_the_doghouse(o)
if __name__ == "__main__": main()