形状工厂-lintcode
来源:互联网 发布:腾讯微云网盘for mac 编辑:程序博客网 时间:2024/05/02 00:33
C++代码:
/** * Your object will be instantiated and called as such: * ShapeFactory* sf = new ShapeFactory(); * Shape* shape = sf->getShape(shapeType); * shape->draw(); */class Shape {public: virtual void draw() const = 0;};class Rectangle: public Shape {public: void draw() const { cout<<" ----"<<endl; cout<<"| |"<<endl; cout<<" ----"<<endl; }};class Square: public Shape {public: void draw() const{ cout << " ---- " << endl; cout<<"| |"<<endl; cout<<"| |"<<endl; cout<<" ---- "<<endl; }};class Triangle: public Shape { void draw() const { cout<<" /\\ "<<endl; cout<<" / \\ "<<endl; cout<<"/____\\"<<endl; }};class ShapeFactory {public: /** * @param shapeType a string * @return Get object of type Shape */ Shape* getShape(string& shapeType) { if (shapeType == "Square") { return new Square(); }else if (shapeType == "Triangle") { return new Triangle(); }else if (shapeType == "Rectangle") { return new Rectangle(); } }};
python代码:
"""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): def draw(self): print ' /\\ ' print ' / \\ ' print '/____\\'class Rectangle(Shape): def draw(self): print ' ---- ' print r'| |' print r' ---- 'class Square(Shape): def draw(self): print ' ---- ' print '| |' print '| |' print ' ---- 'class ShapeFactory: # @param {string} shapeType a string # @return {Shape} Get object of type Shape def getShape(self, shapeType): if shapeType == 'Triangle': return Triangle() if shapeType == 'Rectangle': return Rectangle() if shapeType == 'Square': return Square()
0 0
- 形状工厂-lintcode
- lintcode-->形状工厂
- Lintcode: 形状工厂
- 形状工厂
- 形状工厂
- LintCode:玩具工厂
- lintcode-->玩具工厂
- LintCode之玩具工厂
- 玩具工厂-LintCode
- D3形状(二):曲线工厂
- LintCode-玩具工厂(Java工厂模式)
- LintCode-[容易] 496. 玩具工厂
- 497.Shape Factory-形状工厂(容易题)
- C++设计模式 factory工厂模式,实现绘制几何形状
- LintCode
- lintcode--
- lintcode
- LintCode
- Github上传代码教程
- Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library
- git 删除分支和删除文件夹
- 欢迎使用CSDN-markdown编辑器
- 对spring ioc工作原理的理解
- 形状工厂-lintcode
- ListView的基本使用及其优化
- 【PHP高级】新闻管理系统NMS(SQL语句)
- maven生成的项目,更改web.xml头成servlet 3.1配置
- Java之父求职被嫌年纪大:程序员只能吃青春饭?
- Android集成“小米消息推送”详细教程
- ueditor 后端配置项没有正常加载
- android JNI 错误“Cannot load library: soinfo_relocate”
- java获取文件大小