Java泛型从类创建数组

时间:2022-09-25 10:11:05

I have a hierarchy where Square, Triangle and Circle all extend from Shape. I have a working method:

我有一个层次结构,其中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();

}

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:

现在我想使这个方法通用,以便它可以接受任何形状。我希望能够将其称为someMethod(Triangle.class,新文件(“File_with_triangles”)或someMethod(Circle.class,新文件(“File_with_circles”)。我正在尝试这样:

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??? #### ???

应该在####那里有什么? #### ???

3 个解决方案

#1


3  

Assuming ThirdPartClass.someMajicMethod has a signature something like this:

假设ThirdPartClass.someMajicMethod有这样的签名:

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.

所以如果你调用someMethod(Triangle.class,file),那么在调用someMajicMethod时,arrayType将是Triangle [] .class。

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.

虽然您可能会发现让someMethod将数组类型作为参数而不是元素类型更简单,因此您可以避免该步骤。

#2


4  

Perhaps Array.newInstance(..) is of interest for you

也许Array.newInstance(..)对你很感兴趣

#3


3  

Shape[] shapes = foo.someMajicMethod(type, file);

If foo is a third-party class, I assume you don't control the API of it. I assumed it has the appropriate method signature to handle the line I've written but there is no way for me to be certain without more information about that class.

如果foo是第三方类,我假设你不控制它的API。我假设它有适当的方法签名来处理我写过的行,但是如果没有关于该类的更多信息,我无法确定。

If this doesn't work, what is the problem?

如果这不起作用,问题是什么?

#1


3  

Assuming ThirdPartClass.someMajicMethod has a signature something like this:

假设ThirdPartClass.someMajicMethod有这样的签名:

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.

所以如果你调用someMethod(Triangle.class,file),那么在调用someMajicMethod时,arrayType将是Triangle [] .class。

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.

虽然您可能会发现让someMethod将数组类型作为参数而不是元素类型更简单,因此您可以避免该步骤。

#2


4  

Perhaps Array.newInstance(..) is of interest for you

也许Array.newInstance(..)对你很感兴趣

#3


3  

Shape[] shapes = foo.someMajicMethod(type, file);

If foo is a third-party class, I assume you don't control the API of it. I assumed it has the appropriate method signature to handle the line I've written but there is no way for me to be certain without more information about that class.

如果foo是第三方类,我假设你不控制它的API。我假设它有适当的方法签名来处理我写过的行,但是如果没有关于该类的更多信息,我无法确定。

If this doesn't work, what is the problem?

如果这不起作用,问题是什么?