Shape.java
public abstract class Shape
{
abstract void draw();
}
Circle.java
public class Circle extends Shape
{ @Override
public void draw()
{
System.out.println("draw a circle!");
}
}
Line.java
public class Line extends Shape
{ @Override
public void draw()
{
System.out.println("draw a line!");
}
}
Rectangle.java
public class Rectangle extends Shape
{ @Override
public void draw()
{
System.out.println("draw a Rectangle!");
}}
ShapeFactory.java
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;
}
}
}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();
}}运行shape一直为空,我不知道什么原因,检查好久了!
public abstract class Shape
{
abstract void draw();
}
Circle.java
public class Circle extends Shape
{ @Override
public void draw()
{
System.out.println("draw a circle!");
}
}
Line.java
public class Line extends Shape
{ @Override
public void draw()
{
System.out.println("draw a line!");
}
}
Rectangle.java
public class Rectangle extends Shape
{ @Override
public void draw()
{
System.out.println("draw a Rectangle!");
}}
ShapeFactory.java
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;
}
}
}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();
}}运行shape一直为空,我不知道什么原因,检查好久了!
类名要全路径
初始化时map的key所用的Integer引用值和shapes.get(new Integer(type))中new Integer(type)的引用值是不一样的new会生成新的引用值,不能随便用的
这句话请楼主改正,因为如果你输入的是字符串,而不是数字,是无法转换成整型的。会有NumberFormateException运行测试,除了这个错,我还没有报任何错。
这个 className 你再在前边加上你的几个类的包名就可以了
你再试试,另外你再看看Integer的几个方法的用处。楼主的用法没有任何错误
我指的字符串,并不是纯数字的字符串,请ls能够理解清楚。
提示输入:
此时如果你输入的是:f348gr 之类的只要不是单纯数字的字符串,NumberFormatException。
楼主再看看 如我的包名是 com.hisoft.othersource 我的代码就是:
return (Shape) Class.forName("com.hisoft.othersource." + className).newInstance
要这样写,我草。
谢谢上面的各位,上面几位说的全路径,提醒了我,跑去看API。!