Java泛型从类创建数组 [英] Java Generics Creating Array from Class
问题描述
我有一个层次结构,Square,Triangle和Circle都从Shape延伸出来。我有一个工作方法:
public void someMethod(){
File file = new File(File_with_squares) ;
ThirdPartyClass foo = new ThirdPartyClass();
Square [] squares = foo.someMajicMethod(Square []。class,file);
for(Square square:squares)
square.draw();
$ / code>
现在我想让这个方法是通用的,接受任何形状。我希望能够将它称为 someMethod(Triangle.class,new File(File_with_triangles)
或 someMethod(Circle.class,new File( File_with_circles)
。我想这样:
public void someMethod(Class< ;? extends Shape> type,File shapeFile){
ThirdPartyClass foo = new ThirdPartyClass();
####这里有什么??? ####
for(Shape shape:shapes)
shape.draw();
}
## ## ##这里有什么??? #### ???
假设ThirdPartClass.someMajicMethod有一个这样的签名:
public T someMajicMethod(Class< T> class1,File file);
然后你应该可以这样做:
public void someMethod(Class< ;? extends Shape> type,File shapeFile){
。
ThirdPartyClass foo = new ThirdPa rtyClass();
@SuppressWarnings(unchecked)
Class <? extends Shape []> arrayType =
(Class< ;? extends Shape []>)Array.newInstance(type,0).getClass();
assert Shape []。class.isAssignableFrom(arrayType);
Shape [] shapes = foo.someMajicMethod(arrayType,shapeFile);
for(Shape shape:shapes)
shape.draw();
$ b所以如果你调用
someMethod(Triangle.class,文件)
,然后arrayType
将在Triangle []。class
code> someMajicMethod
尽管您可能会发现使someMethod将数组类型作为参数而不是元素类型所以你可以避免这一步。
I have a hierarchy where Square, Triangle and Circle all extend from Shape. I have a working method:
public void someMethod() { File file = new File("File_with_squares"); ThirdPartyClass foo = new ThirdPartyClass(); Square[] squares = foo.someMajicMethod(Square[].class,file); for (Square square: squares) square.draw(); }
Now I want to make this method generic so that it can accept any shape. I want to be able to call it
someMethod(Triangle.class,new File("File_with_triangles")
orsomeMethod(Circle.class, new File("File_with_circles")
. I am trying like this:public void someMethod(Class<? extends Shape> type, File shapeFile) { ThirdPartyClass foo = new ThirdPartyClass(); #### What goes here??? #### for (Shape shape: shapes) shape.draw(); }
What should be there at #### What goes here??? #### ???
解决方案Assuming ThirdPartClass.someMajicMethod has a signature something like this:
public <T> T someMajicMethod(Class<T> class1, File file);
Then you should be able to do something like this:
public void someMethod(Class<? extends Shape> type, File shapeFile) { ThirdPartyClass foo = new ThirdPartyClass(); @SuppressWarnings("unchecked") Class<? extends Shape[]> arrayType = (Class<? extends Shape[]>) Array.newInstance(type, 0).getClass(); assert Shape[].class.isAssignableFrom(arrayType); Shape[] shapes = foo.someMajicMethod(arrayType, shapeFile); for (Shape shape: shapes) shape.draw(); }
So if you call
someMethod(Triangle.class, file)
, thenarrayType
will beTriangle[].class
in the call tosomeMajicMethod
.Though you may find it simpler to have someMethod take the array type as a parameter instead of the element type so you can avoid that step.
这篇关于Java泛型从类创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!