呵呵 匿名内部类 本身存在的前提就是内部类需要继承或者实现一个类或者接口。- interface Inter
- {
- void show();
- }
- class Outer
- {
- private int num=2;
- /*class Inner implements Inter
- {
- public void show()
- {
- System.out.println("num="+num);
- }
- }*/
- public void method()
- {
- /*Inner in=new Inner();
- in.show();*/
- new Inter()//匿名内部类其实就是一个匿名子类对象,后面有{},里面定义了成员内容。
- {
- public void show()//对父类中的函数进行覆盖,并进行自定义内容
- {
- System.out.println("num="+num);
- }
- }.show();
- }
- }
- class Demo
- {
- public static void main(String[] args)
- {
- Outer out=new Outer();
- out.method();
- }
- }
复制代码 注释掉的部分 就是内部类的常规写法。 |