Browse Source

Java泛型作业

main
Wangyifei 2 months ago
parent
commit
e0af4a9ae0
  1. 9
      w8/AI协助记录.txt
  2. 74
      w8/Cache.java
  3. 16
      w8/GenericReflect.java
  4. 28
      w8/Pair.java
  5. 30
      w8/README.md
  6. BIN
      w8/运行输出.jpg

9
w8/AI协助记录.txt

@ -0,0 +1,9 @@
# AI协助学习记录
1. Java泛型为什么不能使用基本类型
泛型底层为类型擦除,编译后全部向上转为Object。
基本数据类型不属于对象,无法转为Object,仅能使用包装类。
2. 泛型擦除后为何反射仍可以获取泛型
运行时实例化的泛型会被完全擦除。
书写在类、成员变量、方法上的泛型声明会保留在字节码中。
依靠反射ParameterizedType即可读取原本泛型。

74
w8/Cache.java

@ -0,0 +1,74 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Cache {
private final ConcurrentHashMap<Object,CacheNode> map;
private final long timeOut;
private ScheduledExecutorService thread;
//无参构造永久不过期
public Cache(){
this(0);
}
//有参构造设置过期时间
public Cache(long timeOut){
map = new ConcurrentHashMap<>();
this.timeOut = timeOut;
//定时清理过期数据
thread = Executors.newSingleThreadScheduledExecutor();
thread.scheduleAtFixedRate(this::clearTimeOut,2,2,TimeUnit.SECONDS);
}
//存入缓存
public void put(Object key,Object value){
put(key,value,timeOut);
}
//自定义过期时间存放
public void put(Object key,Object value,long ttl){
long endTime = ttl == 0 ? Long.MAX_VALUE : System.currentTimeMillis()+ttl;
map.put(key,new CacheNode(value,endTime));
}
//获取缓存
public Object get(Object key){
CacheNode node = map.get(key);
if(node == null){
return null;
}
//判断是否过期
if(System.currentTimeMillis()>node.endTime){
map.remove(key);
return null;
}
return node.value;
}
//删除缓存
public void remove(Object key){
map.remove(key);
}
//清理过期数据
private void clearTimeOut(){
map.entrySet().removeIf(e->System.currentTimeMillis()>e.getValue().endTime);
}
//关闭线程防止资源泄露
public void close(){
thread.shutdown();
}
//内部存储节点
private static class CacheNode{
Object value;
long endTime;
public CacheNode(Object value,long endTime){
this.value = value;
this.endTime = endTime;
}
}
//测试运行
public static void main(String[] args) throws InterruptedException {
//设置5秒过期
Cache cache = new Cache(5000);
cache.put("学号","2026001");
System.out.println("存入立刻获取:"+cache.get("学号"));
Thread.sleep(6000);
System.out.println("6秒后获取(已过期):"+cache.get("学号"));
cache.close();
}
}

16
w8/GenericReflect.java

@ -0,0 +1,16 @@
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
public class GenericReflect {
private List<String> list;
public static void main(String[] args) throws NoSuchFieldException {
//利用反射获取被擦除的泛型
Field field = GenericReflect.class.getDeclaredField("list");
Type type = field.getGenericType();
if(type instanceof ParameterizedType){
ParameterizedType parameterizedType = (ParameterizedType) type;
System.out.println("获取到的泛型类型:"+parameterizedType.getActualTypeArguments()[0]);
}
}
}

28
w8/Pair.java

@ -0,0 +1,28 @@
public class Pair {
private Object key;
private Object value;
public Pair(Object key, Object value) {
this.key = key;
this.value = value;
}
//交换键值
public Pair swap(){
return new Pair(value,key);
}
public Object getKey(){
return key;
}
public Object getValue(){
return value;
}
@Override
public String toString() {
return "键:"+key+" 值:"+value;
}
public static void main(String[] args) {
Pair p1 = new Pair("姓名","张三");
System.out.println("交换前:"+p1);
Pair p2 = p1.swap();
System.out.println("交换后:"+p2);
}
}

30
w8/README.md

@ -0,0 +1,30 @@
\# Java泛型作业
&#x20;文件功能
&#x20;1. Pair.java:泛型键值对,实现键值交换
&#x20;2. Cache.java:泛型缓存,支持过期自动清理、线程安全
&#x20;3. GenericReflect.java:反射获取被擦除的泛型
&#x20;编译运行命令
&#x20;编译
&#x20;javac Pair.java
&#x20;javac Cache.java
&#x20;javac GenericReflect.java
&#x20;运行
&#x20;java Pair
&#x20;java Cache
&#x20;java GenericReflect

BIN
w8/运行输出.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

Loading…
Cancel
Save