gpt4 book ai didi

java - 抽象泛型类中的静态变量

转载 作者:行者123 更新时间:2023-11-30 06:33:14 24 4
gpt4 key购买 nike

我有一个名为缓存的类。它是一个通用的抽象类,负责处理永远类型扩展类的全局缓存。我的问题是,如果我在基类下有一个静态变量,那么对于每个扩展类型,静态变量是唯一的还是对于所有扩展 Cache 的类型都是相同的。

例如界面:

Cache<K, V> 
private static Cache<K, V>
[creates a cache store on first load]
static V get(K key);

然后我有一个实现类:

PersonCache extends Cache<String, Person>
void load(String person);

JobCache extends Cache<Integer, Job>
void load(Integer key);

Cache 的静态变量会产生哪些行为。 [get 变量的目的是成为 JobCache/PersonCache 存储的单一公共(public)入口点]每种类型(PersonCache、JobCache)是否都有自己的缓存存储,或者 Cache 会尝试存储它接收到的所有内容?

最佳答案

我认为你做不到。来自Java Language Specification Sec. 8.1.2 :

It is a compile-time error to refer to a type parameter of a class C anywhere in the declaration of a static member of C or the declaration of a static member of any type declaration nested within C. It is a compile-time error to refer to a type parameter of a class C within a static initializer of C or any class nested within C.

关于java - 抽象泛型类中的静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8198443/

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