64

局部内部类-奔跑吧爽爽的博客-51CTO博客

 6 years ago
source link: http://blog.51cto.com/13579086/2066253
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

share_default.jpg

局部内部类

1.什么是局部内部类
定义在方法内部的类叫做局部内部类
2.如何访问局部内部类中的成员
public class Test {

public static void main(String[] args) {    Outer  o = new Outer();    o.method();}

}
class Outer {
int x = 0 ;
void method(){
class Inner{
void function(){
System.out.println("x="+x);
}
}
//调用局部内部类中的方法
Inner inner = new Inner();
inner.function();
}
}
3.内部类定义原则
当描述事物时,事物内部还有事物,该事物用内部类描述,因为内部事物在使用外部事物的内容
例如:心脏这个内部事物在使用人体这个外部事物中的血液


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK