我怎么能不使用参数在不同 类 的方法之间传递值
How can I not use argument to pass values between methods of different classes
一般来说,不同classes之间的值传递是通过方法的参数完成的,例如,传递id值是这样的:
public class Controller {
private Service service = new Service();
void controllerMethod() {
Integer id = 5;
service.serviceMethod(id);
}
public static void main(String[] args) {
Controller c = new Controller();
c.controllerMethod();
}
}
public class Service {
private Dao dao = new Dao();
void serviceMethod(Integer id) {
dao.daoMethod(id);
}
}
public class Dao {
void daoMethod(Integer id) {
System.out.println(id);
}
}
这些方法调用实际上是在一个线程中,"main"线程中都有。所以我想知道我是否可以将这个值存储在class'Controller'中的ThreadLocal中,然后从class'Dao'中的ThreadLocal中获取它,但我失败了。
public class Controller {
private Service service = new Service();
void controllerMethod() {
Integer id = 5;
System.out.println(Thread.currentThread().getName());
// ----add code
ThreadLocal<Integer> local = new ThreadLocal<>();
local.set(id);
// ----
service.serviceMethod();
}
public static void main(String[] args) {
Controller c = new Controller();
c.controllerMethod();
}
}
public class Service {
private Dao dao = new Dao();
void serviceMethod() {
System.out.println(Thread.currentThread().getName());
dao.daoMethod();
}
}
public class Dao {
void daoMethod() {
System.out.println(Thread.currentThread().getName());
// ----add code
ThreadLocal<Integer> local = new ThreadLocal<>();
System.out.println(local.get());
// ----
}
}
结果:
不知道为啥...这种方法真的可以实现无参传值吗?我希望有人能帮助我。
ps:
Is there any other way to implement a no-arguments transfer of values between multiple classes?
一个ThreadLocal
通常用作静态变量:
public class Main {
private static final ThreadLocal<Integer> CURRENT_ID = new ThreadLocal<>(1);
private void method1() {
CURRENT_ID.set(100);
method2();
}
private void method2() {
System.out.println("ID: " + CURRENT_ID.get());
}
}
但对于常规应用程序代码,您几乎不需要直接使用 ThreadLocal
。即使它使编写代码更容易,也请始终牢记 testing!所以在你的情况下,我会考虑继续传递必要的变量作为参数。
如果您考虑过通过 ThreadLocal 传递 DAO 或服务等依赖项,则应该考虑 dependency injection。
当方法的参数太多时,不要通过 ThreadLocal 传递一些参数,请查看 parameter objects。
您可以使用 Java 的系统 Class-@Abhishek 建议的解决方案
System.setProperty("id", Integer.toString(5));
现在您可以使用 getProperty("id") 方法并在另一个 class.
中读取 if id 的值
System.getProperty("id");
ThreadLocal class 为每个线程提供其自己的特定对象实例。它不是用于传递参数,当你有一些不是线程安全的对象但你想避免同步访问该对象时使用它。
这取决于您的参数或变量的访问类型,如果它们在特定方法中是局部的并且它们不是常量并且 return 值或方法主体的代码取决于该变量,则将其作为输入参数该方法的方法,但如果它在整个 class 上是全局的,则将其设为 class 级别变量并使用 getter 和 setter 来访问它,如果 classs 的变量级别在该 class 的所有实例之间共享,然后根据您的设计、场景和要求将其设为静态,您也可以将其设为最终状态或在构造函数或静态代码块中初始化变量。
你可以这样做::
class Test {
public static void main(String[] args) {
System.setProperty("id", "5");
getId();
}
public static void getId() {
System.out.println(System.getProperty("id"));
}
}
您可以设置 属性 并且可以从任何地方访问它。
这是最简单的方法。
一般来说,不同classes之间的值传递是通过方法的参数完成的,例如,传递id值是这样的:
public class Controller {
private Service service = new Service();
void controllerMethod() {
Integer id = 5;
service.serviceMethod(id);
}
public static void main(String[] args) {
Controller c = new Controller();
c.controllerMethod();
}
}
public class Service {
private Dao dao = new Dao();
void serviceMethod(Integer id) {
dao.daoMethod(id);
}
}
public class Dao {
void daoMethod(Integer id) {
System.out.println(id);
}
}
这些方法调用实际上是在一个线程中,"main"线程中都有。所以我想知道我是否可以将这个值存储在class'Controller'中的ThreadLocal中,然后从class'Dao'中的ThreadLocal中获取它,但我失败了。
public class Controller {
private Service service = new Service();
void controllerMethod() {
Integer id = 5;
System.out.println(Thread.currentThread().getName());
// ----add code
ThreadLocal<Integer> local = new ThreadLocal<>();
local.set(id);
// ----
service.serviceMethod();
}
public static void main(String[] args) {
Controller c = new Controller();
c.controllerMethod();
}
}
public class Service {
private Dao dao = new Dao();
void serviceMethod() {
System.out.println(Thread.currentThread().getName());
dao.daoMethod();
}
}
public class Dao {
void daoMethod() {
System.out.println(Thread.currentThread().getName());
// ----add code
ThreadLocal<Integer> local = new ThreadLocal<>();
System.out.println(local.get());
// ----
}
}
结果:
不知道为啥...这种方法真的可以实现无参传值吗?我希望有人能帮助我。
ps: Is there any other way to implement a no-arguments transfer of values between multiple classes?
一个ThreadLocal
通常用作静态变量:
public class Main {
private static final ThreadLocal<Integer> CURRENT_ID = new ThreadLocal<>(1);
private void method1() {
CURRENT_ID.set(100);
method2();
}
private void method2() {
System.out.println("ID: " + CURRENT_ID.get());
}
}
但对于常规应用程序代码,您几乎不需要直接使用 ThreadLocal
。即使它使编写代码更容易,也请始终牢记 testing!所以在你的情况下,我会考虑继续传递必要的变量作为参数。
如果您考虑过通过 ThreadLocal 传递 DAO 或服务等依赖项,则应该考虑 dependency injection。
当方法的参数太多时,不要通过 ThreadLocal 传递一些参数,请查看 parameter objects。
您可以使用 Java 的系统 Class-@Abhishek 建议的解决方案
System.setProperty("id", Integer.toString(5));
现在您可以使用 getProperty("id") 方法并在另一个 class.
中读取 if id 的值 System.getProperty("id");
ThreadLocal class 为每个线程提供其自己的特定对象实例。它不是用于传递参数,当你有一些不是线程安全的对象但你想避免同步访问该对象时使用它。 这取决于您的参数或变量的访问类型,如果它们在特定方法中是局部的并且它们不是常量并且 return 值或方法主体的代码取决于该变量,则将其作为输入参数该方法的方法,但如果它在整个 class 上是全局的,则将其设为 class 级别变量并使用 getter 和 setter 来访问它,如果 classs 的变量级别在该 class 的所有实例之间共享,然后根据您的设计、场景和要求将其设为静态,您也可以将其设为最终状态或在构造函数或静态代码块中初始化变量。
你可以这样做::
class Test {
public static void main(String[] args) {
System.setProperty("id", "5");
getId();
}
public static void getId() {
System.out.println(System.getProperty("id"));
}
}
您可以设置 属性 并且可以从任何地方访问它。 这是最简单的方法。