java是否与C#“using”子句等效
我已经看到在一些C#发布的问题中引用了一个“using”子句。 java有相同的function吗?
是。 Java 1.7引入了try-with-resources构造,允许您编写:
try(InputStream is1 = new FileInputStream("/tmp/foo"); InputStream is2 = new FileInputStream("/tmp/bar")) { /* do stuff with is1 and is2 */ }
……就像using
声明一样。
不幸的是,在Java 1.7之前,Java程序员被迫最终使用try {…} {…}。 在Java 1.6中:
InputStream is1 = new FileInputStream("/tmp/foo"); try{ InputStream is2 = new FileInputStream("/tmp/bar"); try{ /* do stuff with is1 and is 2 */ } finally { is2.close(); } } finally { is1.close(); }
是的,因为Java 7你可以重写:
InputStream is1 = new FileInputStream("/tmp/foo"); try{ InputStream is2 = new FileInputStream("/tmp/bar"); try{ /* do stuff with is1 and is2 */ } finally { is2.close(); } } finally { is1.close(); }
如
try(InputStream is1 = new FileInputStream("/tmp/foo"); InputStream is2 = new FileInputStream("/tmp/bar")) { /* do stuff with is1 and is2 */ }
作为参数传递给try语句的对象应该实现java.lang.AutoCloseable
。看看官方文档 。
对于旧版本的Java,请查看此答案和此答案 。
语言中最接近的等价物是使用try-finally。
using (InputStream in as FileInputStream("myfile")) { ... use in ... }
变
final InputStream in = FileInputStream("myfile"); try { ... use in ... } finally { in.close(); }
请注意,一般forms总是:
acquire; try { use; } finally { release; }
如果采集在try块内,则在采集失败的情况下将释放。 在某些情况下,您可能会遇到不必要的代码(通常在上面的示例中测试null),但是在ReentrantLock的情况下,会发生坏事。
如果你经常做同样的事情,你可以使用“执行”这个习惯用法。 不幸的是,Java的语法很冗长,所以有很多更大胆的板块。
fileInput("myfile", new FileInput() { public Void read(InputStream in) throws IOException { ... use in ... return null; } });
哪里
public static T fileInput(FileInput handler) throws IOException { final InputStream in = FileInputStream("myfile"); try { handler.read(in); } finally { in.close(); } }
更复杂的例子我,例如,换行exception。
不是我知道的。 你可以用try … finally块来模拟,但它仍然不完全相同。
你可以在Java中获得的最接近的是try / finally。 此外,Java不提供隐式的Disposable类型。
C#:将变量置于使用块之外
public class X : System.IDisposable { public void Dispose() { System.Console.WriteLine("dispose"); } private static void Demo() { X x = new X(); using(x) { int i = 1; i = i/0; } } public static void Main(System.String[] args) { try { Demo(); } catch (System.DivideByZeroException) {} } }
Java:在块外部限定变量
public class X { public void dispose() { System.out.println("dispose"); } private static void demo() { X x = new X(); try { int i = 1 / 0; } finally { x.dispose(); } } public static void main(String[] args) { try { demo(); } catch(ArithmeticException e) {} } }
C#:在块内查找变量
public class X : System.IDisposable { public void Dispose() { System.Console.WriteLine("dispose"); } private static void Demo() { using(X x = new X()) { int i = 1; i = i/0; } } public static void Main(System.String[] args) { try { Demo(); } catch (System.DivideByZeroException) {} } }
Java:在块内查找变量
public class X { public void dispose() { System.out.println("dispose"); } private static void demo() { { X x = new X(); try { int i = 1 / 0; } finally { x.dispose(); } } } public static void main(String[] args) { try { demo(); } catch(ArithmeticException e) {} } }
这是很长一段时间,但在Java 7中添加了try-with-resources语句以及AutoCloseable接口。
我认为你可以实现类似于“使用”块的东西,实现一个匿名的内部类。 就像Spring使用“Dao模板”一样。
好吧,无论如何使用语法糖,所以Java研究员,不要冒汗。
如果我们在Java中获得BGGA闭包,那么这也将为Java中的类似结构打开。 Gafter在他的幻灯片中使用了这个例子,例如:
withLock(lock) { //closure }
第一个例子中大多数程序员使用的实际习语如下:
InputStream is1 = null; InputStream is2 = null; try{ is1 = new FileInputStream("/tmp/bar"); is2 = new FileInputStream("/tmp/foo"); /* do stuff with is1 and is 2 */ } finally { if (is1 != null) { is1.close(); } if (is2 != null) { is2.close(); } }
使用这个成语的缩进更少,当你有超过2个资源进行清理时,这变得更加重要。
此外,您可以向结构中添加一个catch子句,以处理新的FileStream()如果需要它将抛出exception。 在第一个示例中,如果要执行此操作,则必须使用另一个封闭的try / catch块。
不,没有。
您可以
public void func(){ { ArrayList l = new ArrayList(); } System.out.println("Hello"); }
这为您提供了using子句的有限范围,但没有任何IDisposable接口来调用终结代码。 您可以使用try {} catch(){}最后{},但它没有使用的糖。 顺便说一句,在Java中使用终结器通常是一个坏主意。
不,在Java中没有使用,最相似的function是“import”关键字。