Files.walk是我应该关闭的流之一,但是,如何在下面的代码中关闭流?下面的代码是否有效,或者我是否需要重写它以便我可以访问流来关闭它?
List<Path> filesList = Files.walk(Paths.get(path)).filter(Files::isRegularFile ).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud) 我不应该能够调用实例化对象的私有方法.我想知道为什么下面的代码有效.
public class SimpleApp2 {
/**
* @param args
*/
private int var1;
public static void main(String[] args) {
SimpleApp2 s = new SimpleApp2();
s.method1(); // interesting?!
}
private void method1() {
System.out.println("this is method1");
this.method2(); // this is ok
SimpleApp2 s2 = new SimpleApp2();
s2.method2(); // interesting?!
System.out.println(s2.var1); // interesting?!
}
private void method2() {
this.var1 = 10;
System.out.println("this is method2");
}
}
Run Code Online (Sandbox Code Playgroud)
我知道可以从类中访问私有方法.但是,如果类中的方法实例化同一个类的对象,那么范围规则是否应该应用于该实例化对象?
可以像main这样的静态方法访问类的非静态成员,如本例所示?