通用Class 中的T类是否可以从另一个类分配?

时间:2022-01-16 07:26:46

Edit Since there were many downvotes and people who didn't understand what I'm asking for, I'll rephrase:

编辑因为有许多downvotes和那些不理解我要求的人,我会改写:

How do I find out at runtime what is the class that foo was generified from?

我如何在运行时发现foo被泛化的类是什么?

public boolean doesClassImplementList(Class<?> genericClass)
{
  // help me fill this in
  // this method should return true if genericClass implements List
  // I can't do this because it doesn't compile:
  return genericClass instanceof List;
}

4 个解决方案

#1


Class.isAssignableFrom

#2


Class<Integer> is a type declaration, you can't assign it to a variable, as you are doing on your first line.

Class 是一个类型声明,您不能像在第一行那样将它分配给变量。

Class<Integer> is basically a way of referencing the Integer type, so you can pass it as a parameter. It does affect some methods (such as cast) but it is basically a way of providing a generic parameter to a method and associate it with a type.

Class 基本上是一种引用Integer类型的方法,因此您可以将其作为参数传递。它确实会影响某些方法(例如强制转换),但它基本上是一种为方法提供泛型参数并将其与类型相关联的方法。

For example:

public <T> T someMethod(Object someObject, Class<T> castTo) {
     return castTo.cast(someObject);
}

#3


you cannot in Java (type erasure)

你不能用Java(类型擦除)

#4


You should be using Class.forName( "fully_qualified_name" ) to generate a Class object, or using the .getClass() method on an already instantiated object. Also, for int types, the class is defined in the Integer.TYPE property.

您应该使用Class.forName(“fully_qualified_name”)生成Class对象,或者在已实例化的对象上使用.getClass()方法。此外,对于int类型,该类在Integer.TYPE属性中定义。

I need a little more clarification of what you are attempting to do, match the same type? You can use a number of methods, what is the end goal?

我需要更多一点澄清你想要做什么,匹配相同的类型?你可以使用多种方法,最终目标是什么?

Ok, now that you've clarified... heres some help ... just pass in a class..

好的,既然你已经澄清了...继承人帮助......只是通过一堂课......

public boolean doesClassImplementList(Class genericClass)
{
  // help me fill this in
  // this method should return true if genericClass implements List
  // I can't do this because it doesn't compile:

  //this will give you a a list of class this class implements
  Class[] classesImplementing = genericClass.getInterfaces();

  //loop through this array and test for:
  for(int i=0; i<classesImplementing.length; i++) {
   if (classesImplementing[i].getName().equals("java.util.List")) {
    return true;
   }
  }
  return false;
}

#1


Class.isAssignableFrom

#2


Class<Integer> is a type declaration, you can't assign it to a variable, as you are doing on your first line.

Class 是一个类型声明,您不能像在第一行那样将它分配给变量。

Class<Integer> is basically a way of referencing the Integer type, so you can pass it as a parameter. It does affect some methods (such as cast) but it is basically a way of providing a generic parameter to a method and associate it with a type.

Class 基本上是一种引用Integer类型的方法,因此您可以将其作为参数传递。它确实会影响某些方法(例如强制转换),但它基本上是一种为方法提供泛型参数并将其与类型相关联的方法。

For example:

public <T> T someMethod(Object someObject, Class<T> castTo) {
     return castTo.cast(someObject);
}

#3


you cannot in Java (type erasure)

你不能用Java(类型擦除)

#4


You should be using Class.forName( "fully_qualified_name" ) to generate a Class object, or using the .getClass() method on an already instantiated object. Also, for int types, the class is defined in the Integer.TYPE property.

您应该使用Class.forName(“fully_qualified_name”)生成Class对象,或者在已实例化的对象上使用.getClass()方法。此外,对于int类型,该类在Integer.TYPE属性中定义。

I need a little more clarification of what you are attempting to do, match the same type? You can use a number of methods, what is the end goal?

我需要更多一点澄清你想要做什么,匹配相同的类型?你可以使用多种方法,最终目标是什么?

Ok, now that you've clarified... heres some help ... just pass in a class..

好的,既然你已经澄清了...继承人帮助......只是通过一堂课......

public boolean doesClassImplementList(Class genericClass)
{
  // help me fill this in
  // this method should return true if genericClass implements List
  // I can't do this because it doesn't compile:

  //this will give you a a list of class this class implements
  Class[] classesImplementing = genericClass.getInterfaces();

  //loop through this array and test for:
  for(int i=0; i<classesImplementing.length; i++) {
   if (classesImplementing[i].getName().equals("java.util.List")) {
    return true;
   }
  }
  return false;
}