Java泛型从类创建数组 [英] Java Generics Creating Array from Class

查看:77
本文介绍了Java泛型从类创建数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个层次结构,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") or someMethod(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), then arrayType will be Triangle[].class in the call to someMajicMethod.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆