1、概念
将类定义在其他类的内部的类称为内部类,其他类叫做外部类。定义在其他类成员变量位置的类叫做成员内部类,定义在局部变量位置的内部类叫做局部内部类。
2、成员内部类
定义格式:
class 外部类{
修饰符 class 内部类{
其他代码;
}
}
访问方式:
外部类名.内部类名 变量名 = new 外部类名().new 内部类名();
3、局部内部类
定义格式:
class 外部类名 {
修饰符 返回值类型 方法名(参数列表){
修饰符 class 内部类名 {
其他代码;
}
}
}
访问方式:
在外部类方法中,创建内部类对象,进行访问
4、匿名内部类的使用