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

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

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

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

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

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)
{
//....;
}

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

7tofc5zh1#

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

  1. public Class<? extends Something> getSomeClass() {
  2. return SomethingSpecific.class;
  3. }
  4. public void convertToGenericsAndAssign() {
  5. Class<? extends Something> str = getSomeClass();
  6. // you are getting a class object. You need to actually store a class to make it work.
  7. passString(str);
  8. }
  9. public <D extends Something> void passString(Class<D> strType) {
  10. // ....;
  11. }
  12. public static class Something {}
  13. public static class SomethingSpecific extends Something {}

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

  1. public Class<? extends Something> getSomeClass() {
  2. return SomethingSpecific.class;
  3. }
  4. public void convertToGenericsAndAssign() {
  5. Class<? extends Something> someClass = getSomeClass();
  6. // try to get an parameter free constructor:
  7. try {
  8. Constructor<? extends Something> constructor = someClass.getConstructor();
  9. Something instance = constructor.newInstance();
  10. // Be aware, that the generic type `D` i smore or less just `Something`,
  11. // because the compiler cannot know at compile time, what subclass of `Something`
  12. // D should actually be. So it can just induce `Something`.
  13. passSomething(instance);
  14. } catch (NoSuchMethodException e) {
  15. // Handle , if the class does not have an empty constructor.
  16. e.printStackTrace();
  17. } catch (SecurityException e) {
  18. // Handle, if the constructor is not accessible, or if the
  19. // VM does not allow reflection.
  20. e.printStackTrace();
  21. } catch (InstantiationException e) {
  22. // Handle, if the instance failed to be created.
  23. e.printStackTrace();
  24. } catch (IllegalAccessException e) {
  25. // Handle, if the metohd is e.g. private or something.
  26. e.printStackTrace();
  27. } catch (IllegalArgumentException e) {
  28. // This should not be thrown, as you pass no arguments, but
  29. // you still have to catch it.
  30. e.printStackTrace();
  31. } catch (InvocationTargetException e) {
  32. // If the constructor threw an internal exception.
  33. e.printStackTrace();
  34. }
  35. }
  36. public <D extends Something> void passSomething(D strType) {
  37. // ....;
  38. }
  39. public static class Something {}
  40. public static class SomethingSpecific extends Something {}
展开查看全部

相关问题