Can we call a quot;casequot; inside another case in the same switch statement in Java?(我们可以称之为“案例吗?在 Java 的同一个 switch 语句中的另一个案例中?)
问题描述
我的意图是在同一个 switch 语句中在另一个案例中调用两个案例,
My intention is to call two cases inside another case in the same switch statement,
switch (orderType) {
case 1:
statement 1;
break;
case 2:
statement 2;
break;
case 3:
**call case 1;**
**Call case 2;**
break;
default:
break;`
}
我们可以在 Java 中做到这一点吗?
Can we do that in Java?
推荐答案
虽然不能直接影响switch case,但是可以从一个case调用switch的父方法,传递不同的参数.例如,
Although you cannot influence the switch cases directly, you can call switch's parent method from one case and pass different arguments. For example,
void foo(int param1, String param2, ...) {
switch (param1) {
case 0:
foo(1, "some string");
break;
case 1:
//do something
break;
default:
break;
}
}
这篇关于我们可以称之为“案例"吗?在 Java 的同一个 switch 语句中的另一个案例中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我们可以称之为“案例"吗?在 Java 的同一个 switch 语句中的另一个案例中?
基础教程推荐
- 存储 20 位数字的数据类型 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Struts2 URL 无法访问 2022-01-01
