如何在 Java 中的 Switch Case 中调用多个方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30419212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:36:23  来源:igfitidea点击:

How to Call Multiple Methods in a Switch Case in Java

javamethodsswitch-statement

提问by Mark

I'm learning how to build a table in Java with MVC and I am trying to have a switch case that both performs changes to the data in the model and also calls a method like this

我正在学习如何使用 MVC 在 Java 中构建一个表,我正在尝试创建一个 switch case,它既可以对模型中的数据进行更改,也可以调用这样的方法

 public void update()
    {   model.fireTableDataChanged();   }

to update the data presented on the table.

更新表中显示的数据。

Here is the switch case

这是开关盒

 public Object getValueAt(int row, int col)
    {   switch(col)
        {   case 0: return row;
            case 1:  return car.on(car.stops());
            default: return "";   }  
        }

Any assistance is greatly appreciated and if you need to see more of the code to help you answer my question I will provide it.

非常感谢任何帮助,如果您需要查看更多代码来帮助您回答我的问题,我会提供。

采纳答案by hungryghost

I've formatted your code differently. Hopefully, it makes it easier to understand the multiple statements. Try something like this:

我已经对您的代码进行了不同的格式化。希望它可以更容易地理解多个语句。尝试这样的事情:

public Object getValueAt(int row, int col) {
    switch(col) {   
        case 0:
            // You can add any number of statements here.
            ...
            update();
            return row;
        case 1:
            ...
            update();
            return car.on(car.stops());
        default:
            ...
            update();
            return "";
    }  
}

回答by atk

You seem to be under the incorrect understanding that cases end at the first semicolon. This is incorrect. Cases don't end until you end them with the close brace for the overall switch statement, or until you include a break. Between the case and the end of the case, you can have any number of lines of code that do (pretty much) anything you want.

您似乎对案例以第一个分号结束的错误理解。这是不正确的。案例不会结束,直到您以整个 switch 语句的右大括号结束它们,或者直到您包含一个中断。在案例和案例结束之间,您可以拥有任意数量的代码行,可以(几乎)执行您想要的任何操作。

Think of a switch almost like a function, where the only* way to exit the function is to reach a break statement, return statement, or the close brace at the end, in exactly the same way that you exit functions with return and reaching the end of the function.

把 switch 想象成一个函数,其中退出函数的唯一方法是到达 break 语句、return 语句或末尾的右大括号,与使用 return 退出函数并到达函数结束。

switch(condition) {
    case 1:  fcnOne();
    case 2:  fcnTwoA(); fcnTwoB();
    case 3:  fcnThree; break;
    default: fcnFour();
}

If the condition is 1 then fcnOne() is called. There is no break in fcnOne(), so the code continues on into case 2. This is often called falling through. fcnTwoA() is then called. The code continues to the next instruction, which is to call fcnTwoB(). The next instruction is fcnThree(). Finally, we encounter a break statement, which exits the switch block.

如果条件为 1,则调用 fcnOne()。fcnOne() 中没有中断,因此代码继续进入案例 2。这通常称为失败。然后调用 fcnTwoA()。代码继续执行下一条指令,即调用 fcnTwoB()。下一条指令是 fcnThree()。最后,我们遇到一个 break 语句,它退出 switch 块。



Yes, I am intentionally ignoring exceptions, System.exit(), and return values for non-void functions.

是的,我故意忽略异常、System.exit() 和非空函数的返回值。