包装类的一个小坑
作者:互联网
package org.example.testmap;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Param p = new Param();
// 异常
int o1 = p.get("1");
// 正常
Integer o2 = p.get("2");
System.out.println(o2); // null
}
static class Param {
private final Map<String, Object> m = new HashMap<>();
public <K> K get(String key) {
return (K) m.get(key);
}
}
}
标签:Map,HashMap,get,包装,Param,一个,static,public,小坑 来源: https://blog.csdn.net/themagickeyjianan/article/details/121748674