问题描述:
有一个Shape, rectangle, circle 继承此类;
有一个List, List<Shape>.
现在我想写一个方法,
addMoreToList(ArrayList<Shape> list, int i){
Class c = list.get(0).getClass();
Shape s = (Shape) c.newInstance()
list.add(s);
}
往list里面添加i个新对象。比如本身是list<circle>, i = 1, 那么再加入一个circle对象。
我是这么想的: 如果用反射,在方法里获得具体的shape类是可以实现的,
Class c = list.get(0).getClass();
但是接下来生成对象怎么写?
Shape s = (Shape) c.newInstance()
这样写的话,生成的还是shape,并不是具体的circle或者 rectangle.
求指教
--
修改:tortelee FROM 223.166.166.*
FROM 223.166.166.*