LintCode Q497 Shape Factory in Python

  • Jinhai ZHOU
  • 5 Minutes
  • 2017年2月16日
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
"""
Your object will be instantiated and called as such:
sf = ShapeFactory()
shape = sf.getShape(shapeType)
shape.draw()
"""
class Shape:
def draw(self):
raise NotImplementedError('This method should have implemented.')
class Triangle(Shape):
# Write your code here
def draw(self):
print ' /\\\n / \\\n/____\\\n'
class Rectangle(Shape):
# Write your code here
def draw(self):
print ' ----\n| |\n ----'
class Square(Shape):
# Write your code here
def draw(self):
print ' ----\n| |\n| |\n ----'
class ShapeFactory:
# @param {string} shapeType a string
# @return {Shape} Get object of type Shape
def getShape(self, shapeType):
# Write your code here
if shapeType is None:
return None
elif shapeType == 'Square':
return Square()
elif shapeType == 'Triangle':
return Triangle()
elif shapeType == 'Rectangle':
return Rectangle()
else:
return None
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。