gpt4 book ai didi

java - 推断 Java 泛型接口(interface)中类参数的类型

转载 作者:行者123 更新时间:2023-11-30 01:57:58 25 4
gpt4 key购买 nike

请注意:我花了一整天的时间确保这不是重复的。我见过的大多数解决方案都依赖于调用 clazz.getGenericSuperclass() ,然后调用 getActualTypeArguments(),但这并不能解决我的问题,因为通用父类(super class)是java.lang.Object

请继续阅读。

<小时/>

现在来解决问题

我想实现一个提供通用接口(interface)的库。

public interface MyInterface<K, V> {
public V get(K key);
public void set(K key, V value);
}

然后用户可以在具体的类中实现它,例如:

import java.util.HashMap;

public class ConcreteClass implements MyInterface<String, Integer> {
private HashMap< String, Integer > myMap;

public ConcreteClass() {
this.myMap = new HashMap< >();
}

public Integer get(String key) {
return myMap.get(key);
}

public void set(String key, Integer value) {
myMap.put(key, value);
}
}

或者他们可能会实现另一个泛型类,例如:

import java.util.HashMap;

public class GenericClass<K, V> implements MyInterface<K, V> {
private HashMap<K, V> myMap;

public GenericClass() {
this.myMap = new HashMap<K, V>();
}

public V get(K key) {
return myMap.get(key);
}

public void set(K key, V value) {
myMap.put(key, value);
}
}

现在,我希望能够推断这些类的任何实例的泛型类型。我试图在 Main.java 中的 inferTypes 方法中实现此目的,如下所示:

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class Main {
private static <K, V> void inferTypes(MyInterface<K, V> mi) {
Class<?> clazz = mi.getClass();

// Should print whether "ConcreteClass" or "GenericClass"
System.out.println(clazz.getName());

Type[] ifaces = clazz.getGenericInterfaces();
for (Type iface : ifaces) {
// Should print "MyInterface<String, Integer>"
System.out.println(iface.toString());
Type[] types = ((ParameterizedType) iface).getActualTypeArguments();
for (Type type : types) {
// Should print "String" and then "Integer"
System.out.println(type.toString());
}
}
}

public static void main(String[] args) {
// Someone instantiates the class
ConcreteClass cc = new ConcreteClass();
// And I can successfully infers the type
Main.inferTypes(cc);

System.out.println("-------");

// Someone instantiates the generic class
GenericClass<String, Integer> gc = new GenericClass<>();
// And I can't infer the types this time
Main.inferTypes(gc);
}
}

它适用于 ConcreteClass 的实例,但不适用于 GenericClass 的实例。

ConcreteClass
MyInterface<java.lang.String, java.lang.Integer>
class java.lang.String
class java.lang.Integer
-------
GenericClass
MyInterface<K, V>
K
V

我不知道如何获取第二种情况的具体类。任何帮助将不胜感激。

最佳答案

GenericClass<K, V>为例类文件中没有确定类型的信息。这是由于类型删除所致。

您能做的最好的事情就是从 Map 的内容推断类型。如果需要记录类型可以这样做

public class GenericClass<K, V> implements MyInterface<K, V> {
private final Map<K, V> myMap;
private final Class<K> keyClass;
private final Class<V> valueClass

public GenericClass(Class<K> keyClass, Class<V> valueClass) {
this.myMap = new HashMap<K, V>();
}

public Class<K> getKeyClass() { return keyClass; }
public Class<V> getValueClass() { return valueClass; }

即您需要显式存储类型。

关于java - 推断 Java 泛型接口(interface)中类参数的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53742215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com