题
我已经看到的参考在一些C#贴问题的"使用"条款。不java有同等的?
解决方案
是。 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 */
}
...就像使用语句的一样。
不幸的是,在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
。看看官方文档。
语言中最接近的等价物是使用try-finally。
using (InputStream in as FileInputStream("myfile")) {
... use in ...
}
变为
final InputStream in = FileInputStream("myfile");
try {
... use in ...
} finally {
in.close();
}
请注意,一般表格始终为:
acquire;
try {
use;
} finally {
release;
}
如果获取位于try块内,则在采集失败的情况下将释放。在某些情况下,您可能会遇到不必要的代码(通常在上面的示例中测试null),但是在ReentrantLock的情况下会发生坏事。
如果你经常做同样的事情,你可以使用“执行周围”。成语。不幸的是,Java的语法很冗长,所以有很多更大胆的板块。
fileInput("myfile", new FileInput<Void>() {
public Void read(InputStream in) throws IOException {
... use in ...
return null;
}
});
,其中
public static <T> T fileInput(FileInput<T> handler) throws IOException {
final InputStream in = FileInputStream("myfile");
try {
handler.read(in);
} finally {
in.close();
}
}
更复杂的例子,例如,包装异常。
不是我知道的。你可以通过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) {}
}
}
这是一个很长一段时间来,但与Java7 尝试与资源的声明 加入,随着 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()如果需要则抛出异常。在第一个示例中,如果要执行此操作,则必须使用另一个封闭的try / catch块。
没有。
你可以
public void func(){
{
ArrayList l = new ArrayList();
}
System.out.println("Hello");
}
这为您提供了using子句的有限范围,但没有任何IDisposable接口来调用终结代码。您可以使用try {} catch(){}最后{},但它没有使用的糖。顺便说一句,在Java中使用终结器通常是一个坏主意。
不,在Java中没有使用,最相似的功能是“导入”。关键字。