带有泛型的Java静态类Singleton [英] java static class Singleton with generic

查看:49
本文介绍了带有泛型的Java静态类Singleton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使单例显示如下,但是我不断收到警告.如果可能的话,我不想抑制警告.有办法吗?

就目前而言,我不想考虑线程安全性.我只想通过这个警告.

 公共接口Storage< K,V>{公共无效推销(K密钥,V值);公共V get(K键);}公共静态类DefaultStorage< K,V>实现Storage< K,V>{私有Map< Object,Object>map = new ConcurrentHashMap< Object,Object>();私有静态DefaultStorage< K,V>defaultStorage;私人DefaultStorage(){//}public static DefaultStorage<?,?>getInstance(){如果(defaultStorage == null){defaultStorage =新的DefaultStorage();}返回defaultStorage;}} 

谢谢.

解决方案

DefaultStorage 中的变量 defaultStorage 仅在 DefaultStorage的每个实例中都存在一次组合.在运行时,只有一个实际的类和一个静态变量.因此,变量将同时是 DefaultStorage< K1,V1> DefaultStorage< K2,V2> DefaultStorage< K3,V3> >,依此类推.因此,一类将在其中存储 Strings ,另一类将存储 BigDecimals ,另一类将存储 X501Principals .这会破坏类型安全性.

警告是您将原始类型的实例 new DefaultStorage()存储在声明为 DefaultStorage< K,V> 的变量中.

来自 Angelika Langer的泛型常见问题解答

泛型类型可以具有静态成员吗​​?

是的

泛型类型可以具有静态成员,包括静态字段,静态方法和静态嵌套类型.这些静态成员中的每个静态成员每个封闭类型都存在一次,也就是说,与封闭类型的对象数量无关,并且与程序中某个地方可以使用的泛型类型的实例化数量无关.静态成员的名称(通常是静态成员的)由作用域(包和封装类型)和成员的名称组成.如果封闭类型是通用类型,则作用域限定中的类型必须是原始类型,而不是参数化类型.

I am trying to make a singleton like the following, but I keep getting a warning. If possible, I don't want suppress warning. Is there a way to do it?

For now, I don't want to think about the thread-safety. I just want to pass this warning.

    public interface Storage<K, V> {
        public void put(K key, V value);
        public V get(K key);
    }


    public static class DefaultStorage<K, V> implements Storage<K, V> {

        private Map<Object, Object> map = new ConcurrentHashMap<Object, Object>();

        private static DefaultStorage<K, V> defaultStorage;

        private DefaultStorage() {
            //
        }

        public static DefaultStorage<?, ?> getInstance() {
            if (defaultStorage== null) {
                defaultStorage= new DefaultStorage();
            }
            return defaultStorage;
        }
     }

Thanks.

解决方案

The variable defaultStorage in DefaultStorage only exists once in every instance of a DefaultStorage combined. At runtime there's only one actual class, and one static variable. So, the variable will simultaneously be a DefaultStorage<K1, V1>, a DefaultStorage<K2, V2>, a DefaultStorage<K3, V3>, and so on. So, one class will store Strings in it, another will store BigDecimals, and another X501Principals. This subverts type-safety.

The warning is that you are storing an instance of a raw type, new DefaultStorage(), in a variable declared as DefaultStorage<K, V>.

From Angelika Langer's Generics FAQ,

Can generic types have static members?

Yes.

Generic types can have static members, including static fields, static methods and static nested types. Each of these static members exists once per enclosing type, that is, independently of the number of objects of the enclosing type and regardless of the number of instantiations of the generic type that may be used somewhere in the program. The name of the static member consists - as is usual for static members - of the scope (packages and enclosing type) and the member's name. If the enclosing type is generic, then the type in the scope qualification must be the raw type, not a parameterized type.

这篇关于带有泛型的Java静态类Singleton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆