class Outer{
public void Method(){
int i=10;
System.out.println(i);
Class InsideMethod{
//
}
}
Run Code Online (Sandbox Code Playgroud)
问题:如何在方法之外调用InsideMethod对象
这个片段说明了各种可能性:
public class Outer {
void onlyOuter() { System.out.println("111"); }
void common() { System.out.println("222"); }
public class Inner {
void common() { System.out.println("333"); }
void onlyInner() {
System.out.println("444");// Output: "444"
common(); // Output: "333"
Outer.this.common(); // Output: "222"
onlyOuter(); // Output: "111"
}
}
}
Run Code Online (Sandbox Code Playgroud)
注意:
common();调用从内部类调度实现.OuterClass.this构造指定您要从外部类调度方法(绕过隐藏)onlyOuter()调度方法,OuterClass因为这是定义此方法的最内层封闭类.| 归档时间: |
|
| 查看次数: |
353 次 |
| 最近记录: |