T通过通用接口解析类型。例如
public interface SomeInterface<T> {}public class SomeImplementation implements SomeInterface<String> { public Class getGenericInterfaceType(){ Class clazz = getClass(); ParameterizedType parameterizedType = (ParameterizedType) clazz.getGenericInterfaces()[0]; Type[] typeArguments = parameterizedType.getActualTypeArguments(); Class<?> typeArgument = (Class<?>) typeArguments[0]; return typeArgument; }}public static void main(String[] args) { SomeImplementation someImplementation = new SomeImplementation(); System.out.println(someImplementation.getGenericInterfaceType());}PS:请记住acutalTypeArguments是type
Type。他们一定不是一个
Class。在您的情况下,它是一个Class,因为您的类型定义是
EventHandler<MyEvent>。



