本周学习
内部类,
复习
1.非静态内部类
(1)非静态内部类必须寄存在一个外部类对象里。如果有一个非静态内部类对象那么一定存在对应的外部类对象。非静态内部类对象单独属于外部类的某个对象。
(2)非静态内部类可以直接访问外部类的成员,但是外部类不能直接访问非静态内部类的成员。
(3)非静态内部类不能有静态方法、静态属性、静态初始化块。
(4)外部类的静态方法,静态代码块不能访问非静态内部类,包括不能使用非静态内部类定义变量,创建实例。
public class Test{
public static void main(String[] args){
Outer.Inner a=new Outer().new Inner();
a.show();
}
}
class Outer{
private int age=10;
class Inner{
int age=20;
public void show(){
int age=30;
System.out.println("外部类的成员变量age"+Outer.this.age);
System.out.println("内部类的成员变量age"+this.age);
System.out.println("局部变量age"+age);
}
}
}
2.匿名内部类
只使用一次的类,比如键盘监听操作等
interface A{
void xxx();
}
public static void test(A e){}
public class Test{
public static void main(String[] args){
test(new A(){
public void xxx(){}
});
}
}
预习
异常