Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中对interface对象使用try with resources语句_Java_Try With Resources - Fatal编程技术网

如何在Java中对interface对象使用try with resources语句

如何在Java中对interface对象使用try with resources语句,java,try-with-resources,Java,Try With Resources,我想使用try with resources语句将接口对象定义为一个具体的类。下面是一些松散定义接口和类的示例代码 interface IFoo extends AutoCloseable { ... } class Bar1 implements IFoo { ... } class Bar2 implements IFoo { ... } class Bar3 implements IFoo { ... } // More Bar classes...

我想使用
try with resources
语句将接口对象定义为一个具体的类。下面是一些松散定义接口和类的示例代码

interface IFoo extends AutoCloseable
{
    ...
}

class Bar1 implements IFoo
{
    ...
}

class Bar2 implements IFoo
{
    ...
}

class Bar3 implements IFoo
{
    ...
}

// More Bar classes.........
现在我需要定义一个
IFoo
对象,但是具体的类是以代码的另一个变量为条件的。所有具体类的逻辑都是相同的。因此,我想使用
try with resources
语句来定义接口对象,但我需要使用条件语句来查看需要将接口对象定义为哪个具体类

从逻辑上讲,这就是我想要做的:

public void doLogic(int x)
    try (
        IFoo obj;
        if (x > 0) { obj = new Bar1(); }
        else if (x == 0) { obj = new Bar2(); }
        else { obj = new Bar3(); }
    )
    {
        // Logic with obj
    }
}
我找到的唯一与此相关的资源是@Denis的问题: 然而,这里给出的解决方案需要在我的场景中使用嵌套的三元语句,这很快就会变得一团糟


有人知道这个问题的优雅解决方案吗?

定义一个工厂方法来创建
IFoo
实例:

IFoo createInstance(int x) {
    if (x > 0) { return new Bar1(); }
    else if (x == 0) { return new Bar2(); }
    else { return new Bar3(); }
}
然后在try with resources初始值设定项中调用:

public void doLogic(int x) {
  try (IFoo ifoo = createInstance(x)) {
    // Logic with obj
  }
}

定义工厂方法以创建
IFoo
实例:

IFoo createInstance(int x) {
    if (x > 0) { return new Bar1(); }
    else if (x == 0) { return new Bar2(); }
    else { return new Bar3(); }
}
然后在try with resources初始值设定项中调用:

public void doLogic(int x) {
  try (IFoo ifoo = createInstance(x)) {
    // Logic with obj
  }
}

我同意最好的解决方案是编写一个helper方法,如答案中所示

然而,我还想指出,嵌套的三元运算符并不凌乱。您根本不需要括号,只要格式良好,就可以使其看起来像一个
开关
语句:

try (IFoo foo = x > 20     ? new Bar1() :
                x < 0      ? new Bar2() :
                x == 10    ? new Bar3() :
                x % 2 == 0 ? new Bar4() : 
                             new Bar5()) {
        // do stuff
}
try(IFoo-foo=x>20?新建Bar1():
x<0?新的Bar2()
x==10?新的Bar3()
x%2==0?新的Bar4()
新的Bar5(){
//做事
}

我同意最好的解决方案是编写一个helper方法,如answer中所示

然而,我还想指出,嵌套的三元运算符并不凌乱。您根本不需要括号,只要格式良好,就可以使其看起来像一个
开关
语句:

try (IFoo foo = x > 20     ? new Bar1() :
                x < 0      ? new Bar2() :
                x == 10    ? new Bar3() :
                x % 2 == 0 ? new Bar4() : 
                             new Bar5()) {
        // do stuff
}
try(IFoo-foo=x>20?新建Bar1():
x<0?新的Bar2()
x==10?新的Bar3()
x%2==0?新的Bar4()
新的Bar5(){
//做事
}
我将createInstance(…)作为静态方法添加到我的界面中,效果非常好。谢谢我将createInstance(…)作为一个静态方法添加到我的接口中,效果非常好。谢谢