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

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

作者: 综合楼 | 来源:发表于2021-06-27 19:29 被阅读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();
}

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/mfiyjltx.html