第 62 条:如果其他类型更适合,则尽量避免使用字符串

2021-06-27  本文已影响0人  综合楼
如果其他类型更适合,则尽量避免使用字符串.jpeg
// Broken - inappropriate use of string as capability!
public class ThreadLocal {
    private ThreadLocal() { } // Noninstantiable
    // Sets the current thread's value for the named variable.
    public static void set(String key, Object value);
    // Returns the current thread's value for the named variable.
    public static Object get(String key);
}



public class ThreadLocal {
    private ThreadLocal() { } // Noninstantiable
    public static class Key { // (Capability)
        Key() { }
    }
    // Generates a unique, unforgeable key
    public static Key getKey() {
        return new Key();
    }
    public static void set(Key key, Object value);
    public static Object get(Key key);
}
public final class ThreadLocal {
    public ThreadLocal();
    public void set(Object value);
    public Object get();
}

public final class ThreadLocal<T> {
    public ThreadLocal();
    public void set(T value);
    public T get();
}
上一篇 下一篇

猜你喜欢

热点阅读