java—如何将类< ?>转换为泛型类型

lnvxswe2  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(380)

关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。

5天前关门了。
改进这个问题
我有一个方法返回 Class<? extends Animal> ```
public Class<? extends Animal> getAnimal(Animal animal)
{
//....
}

在参数中接受泛型的另一个方法 `passGenericAnimal(D animal);` 我想要一个动物类的类型 `getAnimal()` 方法,我只想传递给 `passGenericAnimal()` 方法将其转换为泛型类型。
这样地:

public Class<? extends Animal> getAnimal()
{
return Lion.class;
}

public void convertToGenericsAndAssign()
{
T animal = getAnimal();
//getting error here
//Type mismatch: cannot convert from Class<capture#15-of ? extends Animal> to T
passString(animal);
}

public void passGenericAnimal(D animal)
{
//....;
}

有没有办法把类类型转换成泛型类型?
7tofc5zh

7tofc5zh1#

我可以给你一些如何编译的提示,但我怀疑你在任何情况下都走错了方向。即使您的代码编译(并运行)了,它也可能不会做您认为它会做的事情。然而,就目前而言,只是编译方面的变化:

public Class<? extends Something> getSomeClass() {
    return SomethingSpecific.class;
}

public void convertToGenericsAndAssign() {
    Class<? extends Something> str = getSomeClass();
    // you are getting a class object. You need to actually store a class to make it work.
    passString(str);
}

public <D extends Something> void passString(Class<D> strType) {
    // ....;
}

public static class Something {}

public static class SomethingSpecific extends Something {}

我把字体改成 String (不可扩展)到 Something .
正如您在示例中看到的,问题是,您实际上得到了一个 Class 方法中的对象。我不太清楚你为什么期望一个实际对象的示例。如果要示例化该类的对象,则以下代码可能会有所帮助:

public Class<? extends Something> getSomeClass() {
    return SomethingSpecific.class;
}

public void convertToGenericsAndAssign() {
    Class<? extends Something> someClass = getSomeClass();
    // try to get an parameter free constructor:

    try {
        Constructor<? extends Something> constructor = someClass.getConstructor();
        Something instance = constructor.newInstance();

        // Be aware, that the generic type `D` i smore or less just `Something`,
        // because the compiler cannot know at compile time, what subclass of `Something`
        // D should actually be. So it can just induce `Something`.
        passSomething(instance);

    } catch (NoSuchMethodException e) {
        // Handle , if the class does not have an empty constructor.
        e.printStackTrace();

    } catch (SecurityException e) {
        // Handle, if the constructor is not accessible, or if the
        // VM does not allow reflection.
        e.printStackTrace();

    } catch (InstantiationException e) {
        // Handle, if the instance failed to be created.
        e.printStackTrace();

    } catch (IllegalAccessException e) {
        // Handle, if the metohd is e.g. private or something.
        e.printStackTrace();

    } catch (IllegalArgumentException e) {
        // This should not be thrown, as you pass no arguments, but
        // you still have to catch it.
        e.printStackTrace();

    } catch (InvocationTargetException e) {
        // If the constructor threw an internal exception.
        e.printStackTrace();
    }
}

public <D extends Something> void passSomething(D strType) {
    // ....;
}

public static class Something {}

public static class SomethingSpecific extends Something {}

相关问题