相关疑难解决方法(0)

Java - 通用类型 - 类型擦除

我在oracle网站上找到了以下问题和答案

类型擦除后,以下类转换为什么?

public class Pair<K, V> {

    public Pair(K key, V value) {
        this.key = key;
        this.value = value;
    }

    public K getKey(); { return key; }
    public V getValue(); { return value; }

    public void setKey(K key)     { this.key = key; }
    public void setValue(V value) { this.value = value; }

    private K key;
    private V value;
}
Run Code Online (Sandbox Code Playgroud)

回答:

public class Pair {

    public Pair(Object key, Object value) {
        this.key = key;
        this.value = value;
    }

    public Object getKey() …
Run Code Online (Sandbox Code Playgroud)

java generics

4
推荐指数
1
解决办法
683
查看次数

标签 统计

generics ×1

java ×1