ShapeFactory.javaimport java.util.HashMap;
import java.util.Map;
public class ShapeFactory {
public static final int SHAPE_TYPE_CIRCLE=1;
public static final int SHAPE_TYPE_RECTANGLE=2;
public static final int SHAPE_TYPE_LINE=3;
private static Map<Integer,String> Shapes=new HashMap<Integer,String>();
static{
Shapes.put(new Integer(SHAPE_TYPE_CIRCLE),"Circle");
Shapes.put(new Integer(SHAPE_TYPE_RECTANGLE),"Rectangle");
Shapes.put(new Integer(SHAPE_TYPE_LINE),"Line");
}
public static Shape getShape(int type) {
try{
String className=Shapes.get(new Integer(type));
return (Shape)Class.forName(className).newInstance();
}
catch(Exception e) { return null; }
}
}
Panel.java
import java.io.*;
public class Panel {
public void selectShape() throws Exception {
System.out.println("请输入形状类型:");
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
int shapeType=Integer.parseInt(input.readLine());
Shape shape=ShapeFactory.getShape(shapeType);
if(shape==null)
System.out.println("输入的形状类型不存在");
else
shape.draw();
}
public static void main(String[] args) throws Exception {
new Panel().selectShape();
}
}
为何我输入1或2或3都提示该形状不存在?看了好久也看不出来啊!!
import java.util.Map;
public class ShapeFactory {
public static final int SHAPE_TYPE_CIRCLE=1;
public static final int SHAPE_TYPE_RECTANGLE=2;
public static final int SHAPE_TYPE_LINE=3;
private static Map<Integer,String> Shapes=new HashMap<Integer,String>();
static{
Shapes.put(new Integer(SHAPE_TYPE_CIRCLE),"Circle");
Shapes.put(new Integer(SHAPE_TYPE_RECTANGLE),"Rectangle");
Shapes.put(new Integer(SHAPE_TYPE_LINE),"Line");
}
public static Shape getShape(int type) {
try{
String className=Shapes.get(new Integer(type));
return (Shape)Class.forName(className).newInstance();
}
catch(Exception e) { return null; }
}
}
Panel.java
import java.io.*;
public class Panel {
public void selectShape() throws Exception {
System.out.println("请输入形状类型:");
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
int shapeType=Integer.parseInt(input.readLine());
Shape shape=ShapeFactory.getShape(shapeType);
if(shape==null)
System.out.println("输入的形状类型不存在");
else
shape.draw();
}
public static void main(String[] args) throws Exception {
new Panel().selectShape();
}
}
为何我输入1或2或3都提示该形状不存在?看了好久也看不出来啊!!
解决方案 »
- java 一个类中的方法调用另一个类的方法返回空值,怎么解决?
- 看看这段程序run出什么结果,关于Exception的
- 图片放大或缩小的另类处理,谁能帮我解决小第再注册10个用户把分全都送给他!
- jfreecahrt柱子问题
- 紧急求救,在JAVA中如何删除文件中的一行数据???
- Int转Object,小弟刚刚学java?
- 我想定时执行(比如说早上1点)一个java程序,不知道教本怎么写,急,急,谢谢!
- 关于Jbuilder安装以后出现的莫名奇妙的问题
- 可以详细的讲一下java.lang.reflect的实现,我看了半天书也没明白!
- 欢迎大家讨论web下统计报表的实现问题?
- 谁能给一个mdb5加密的java版加密算法?
- 关于StreamTokenizer中的TT_NUMBER
import java.util.HashMap;
import java.util.Map;
public class ShapeFactory {
public static final int SHAPE_TYPE_CIRCLE=1;
public static final int SHAPE_TYPE_RECTANGLE=2;
public static final int SHAPE_TYPE_LINE=3;
private static Map<Integer,String> Shapes=new HashMap<Integer,String>();
static{
Shapes.put(new Integer(SHAPE_TYPE_CIRCLE),"Circle");
Shapes.put(new Integer(SHAPE_TYPE_RECTANGLE),"Rectangle");
Shapes.put(new Integer(SHAPE_TYPE_LINE),"Line");
}
public static Shape getShape(int type) {
try{
String className=Shapes.get(new Integer(type));
return (Shape)Class.forName(className).newInstance();
}
catch(Exception e) { return null; }
}
}abstract class Shape {
abstract void draw();
}class Circle extends Shape {
public void draw() {
System.out.println("draw a circle");
}
}class Line extends Shape {
public void draw() {
System.out.println("draw a line");
}
}class Rectangle extends Shape {
public void draw() {
System.out.println("draw a rectangle");
}
}