【发布时间】:2013-10-05 06:56:04
【问题描述】:
我想创建一个 if 语句来检查哪个方法调用了辅助方法。
我会用伪代码写出我想要的,这样你就能明白我的意思了。
public static void methodOne() {
methodToCall();
}
public static void methodTwo() {
methodToCall();
}
public static void methodThree() {
methodToCall();
}
public static void methodToCall() {
if (methodOne made the call == true) {
execute this
} else if (methodTwo made the call == true){
execute this
} else if (methodThree made the call == true){
execute this
} else {
System.out.println("How did you get here?");
}
}
这就是它的要点。我想要一个简单的检查来查看调用的方法,以便我可以选择与调用相关的操作。
这可能吗?
如果不可能,有解决办法吗?
【问题讨论】:
-
为什么不直接传一个参数?
-
为什么你首先将三个方法调用委托给一个,只是为了再次将它分开?
-
耦合方法(或函数、类、程序...)是必要的,但必须受到控制并尽可能避免。
-
全局变量?你从哪里来的?在 1970 年,好吧,我们别无选择,但今天,使用面向对象语言,全局变量被禁止了!
标签: java if-statement methods