不同开关情况下的变量不能有相同的名称?

JRE*_*REN 6 java variables syntax-error switch-statement

我正在重构一些代码,以便更容易阅读,我遇到了一些我觉得奇怪的东西,我想知道是否有人可以向我解释这个.

原始代码:

if(tokensLeft == 3) {
  String id = tokens.nextToken();
  String value = tokens.nextToken();
  String trailerId = tokens.nextToken();
  rawListener.binaryInfo(id, Integer.parseInt(value), trailerId, this);
} else if(tokensLeft == 2) {
  String id = tokens.nextToken();
  String value = tokens.nextToken();
  rawListener.binaryInfo(id, Integer.parseInt(value), this);
} else {
  System.out.println("Method call binaryInfo could not be done because: \"Wrong number of parameters\"");
}
Run Code Online (Sandbox Code Playgroud)

重构后:

switch(tokensLeft) {
case 3:
  String id = tokens.nextToken();
  String value = tokens.nextToken();
  String trailerId = tokens.nextToken();
  rawListener.binaryInfo(id, Integer.parseInt(value), trailerId, this);
  break;
case 2:
  String id = tokens.nextToken(); // Syntax error
  String value = tokens.nextToken(); // Syntax error
  rawListener.binaryInfo(id, Integer.parseInt(value), this);
  break;
default:
  System.out.println("Method call binaryInfo could not be done because: \"Wrong number of parameters\"");
  break;
}
Run Code Online (Sandbox Code Playgroud)

乍一看,这看起来非常合理,但这给了我一个语法错误.

链接本地重命名的所有引用(不更改其他文件中的引用)

事实证明,出于某些原因,在switch语句中,我无法在另一种情况下再次使用String idString value.

这使我的变量命名相当尴尬.

现在你可以说:"只需在switch语句上面声明你的变量." 但这意味着我总是创建我的变量,即使tokensLeft它既不是3也不是2,我不需要我的变量.这只是感觉使用不必要的记忆.

任何人都可以向我解释为什么开关盒会这样做以及我如何解决我的问题?

NIN*_*OOP 10

您正在重新定义变量,即重复变量声明.case不会阻止.

根据JLS 14:

块是大括号内的语句,本地类声明和局部变量声明语句的序列.

你有两个选择:

  1. 使用每种情况下定义一个显式块{ .. },虽然看起来很奇怪但我必须说.

    要么

  2. 在每个中,case您可以将逻辑委派给方法调用.

  • 然后,您可以将逻辑包含在每个案例的方法调用中? (3认同)

yus*_*ulx 10

添加{}.试试这个:

switch(tokensLeft) {
case 3:
{
  String id = tokens.nextToken();
  String value = tokens.nextToken();
  String trailerId = tokens.nextToken();
  rawListener.binaryInfo(id, Integer.parseInt(value), trailerId, this);
}
  break;
case 2:
{
  String id = tokens.nextToken(); // Syntax error
  String value = tokens.nextToken(); // Syntax error
  rawListener.binaryInfo(id, Integer.parseInt(value), this);
}
  break;
default:
  System.out.println("Method call binaryInfo could not be done because: \"Wrong number of parameters\"");
  break;
}
Run Code Online (Sandbox Code Playgroud)


Bor*_*der 2

完全不声明变量怎么样?

switch (tokensLeft) {
    case 3:
        rawListener.binaryInfo(
                tokens.nextToken(),
                parseInt(tokens.nextToken()),
                tokens.nextToken(),
                this);
        break;
    case 2:
        rawListener.binaryInfo(
                tokens.nextToken(),
                parseInt(tokens.nextToken()),
                this);
        break;
    default:
        throw new IllegalArgumentException("Method call binaryInfo could not be done because: \"Wrong number of parameters\"");
}
Run Code Online (Sandbox Code Playgroud)

我添加了一个static导入Integer.parseInt

最好在命名良好的方法中调用您的逻辑switch,并声明您想要的任何变量:

public void parseTokens() {
    switch (tokensLeft) {
        case 3:
            parseThreeTokens(rawListener, tokens);
            break;
        case 2:
            parseTwoTokens(rawListener, tokens);
            break;
        default:
            throw new IllegalArgumentException("Method call binaryInfo could not be done because: \"Wrong number of parameters\"");
    }
}

public void parseThreeTokens(final RawListener rawListener, final Tokens tokens) {
    final String id = tokens.nextToken();
    final String value = tokens.nextToken();
    final String trailerId = tokens.nextToken();
    rawListener.binaryInfo(id, parseInt(value), trailerId, this);

}

public void parseTwoTokens(final RawListener rawListener, final Tokens tokens) {
    final String id = tokens.nextToken();
    final String value = tokens.nextToken();
    rawListener.binaryInfo(id, parseInt(value), this);
}
Run Code Online (Sandbox Code Playgroud)

  • 我想它会起作用,但当我最初的目标是让它变得更容易时,它似乎让事情变得不那么容易阅读。 (2认同)