我有一个场景,我在代码的多个部分调用特定方法。现在我需要不实现此方法,或者不针对特定条件执行此代码中的行(例如 ON/OFF 开关)。我可以在调用该方法的任何地方添加 if...else 。但是有没有其他方法可以在 java 中做到这一点。也许在方法级别?
正如我所提到的,不想在我调用该方法的任何地方添加我的 if ..else 。在下面添加示例:
public class Example {
static boolean readOnly = true;
public static void getWorkDone() {
System.out.println("I am working to get it done!!");
}
public static void main(String[] args) {
if (readOnly == true)
Example.getWorkDone();
}
public void anotherMethod1() {
if (readOnly == false)
Example.getWorkDone();
}
public void anotherMethod2() {
if (readOnly == true)
Example.getWorkDone();
}
}