抽象泛型类中的静态变量 [英] Static variable in an abstract generic class
问题描述
我有一个称为缓存的类.它是一个通用的抽象类,负责为永远的类型处理全局缓存扩展了该类.我的问题是,如果我在基类下有一个静态变量,则该静态变量在每个扩展类型中是唯一的,还是对于所有扩展Cache的类型而言都是相同的.
I have a class called cache. It is an generic, abstract class responsible for handling the global cache for forever type extends the class. My question is, if I have a static variable under the base class, will the static variable be unique per extending type or will it be the same for all types that extend 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的存储的单个公共入口点]每个类型(PersonCache,JobCache)是否都有自己的缓存存储,还是Cache会尝试存储接收到的所有内容?
Which behavior will be expected from Cache's static variable. [The get variable's intention is to be a single public entry point to the JobCache/PersonCache's store] will each type (PersonCache, JobCache] have its own cache store, or will Cache try to store everything it receives?
推荐答案
我认为您无法做到这一点.摘自 Java语言规范部分.8.1.2 :
I don't think you can do that. From the Java Language Specification Sec. 8.1.2:
在C的静态成员的声明或C中嵌套的任何类型声明的静态成员的声明中的任何地方引用C类的类型参数是编译时错误.引用C的静态初始化程序中的类C的类型参数或嵌套在C中的任何类的时间错误.
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.
这篇关于抽象泛型类中的静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!