LintCode Q496 Toy Factory in Python

  • Jinhai ZHOU
  • 4 Minutes
  • 2017年2月15日
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
"""
Your object will be instantiated and called as such:
ty = ToyFactory()
toy = ty.getToy(type)
toy.talk()
"""
class Toy:
def talk(self):
raise NotImplementedError('This method should have implemented.')
class Dog(Toy):
# Write your code here
def talk(self):
print 'Wow'
class Cat(Toy):
# Write your code here
def talk(self):
print 'Meow'
class ToyFactory:
# @param {string} shapeType a string
# @return {Toy} Get object of the type
def getToy(self, type):
# Write your code here
if type is None:
return None
if type == "Dog":
return Dog()
elif type == "Cat":
return Cat()
return None
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。