从java中的类调用对象

时间:2016-04-06 08:45:41

标签: java

伙计我知道这是虚拟的qustion,但我是初学者..我有这个班级

public class threadLocal {
private static ThreadLocal<String> myThreadLocal;

public threadLocal(){
    myThreadLocal = new ThreadLocal<String>();
}

public static ThreadLocal<String> getMyThreadLocal() {
    return myThreadLocal;
}

public static void setMyThreadLocal(ThreadLocal<String> myThreadLocal) {
    threadLocal.myThreadLocal = myThreadLocal;
}
}

我希望在另一个班级中将其称为

myThreadLocal.setMyThreadLocal("patrick");

那么我应该在threadLocal类中做些什么改变?

1 个答案:

答案 0 :(得分:3)

我认为你应该这样改写你的课程。

public class threadLocal {
 private static ThreadLocal<String> myThreadLocal = new ThreadLocal<String>();

 public static String getMyThreadLocal() {
    return myThreadLocal.get();
  }

 public static void setMyThreadLocal(String str) {
    myThreadLocal.set(str);
  }
}

拨打电话时,您可以致电

threadLocal.getMyThreadLocal()