java-outer单独的类访问匿名类

时间:2014-10-01 09:22:11

标签: java anonymous-class anonymous-inner-class

假设我有以下课程

Class0

public abstract class Class0 {

public Class0(){}
    public void abmethod1(){
        System.out.println("Abstract method 1");
    }

    public void abmethod2(){
        System.out.println("Abstract method 2");
    }
}

的Class1

public class Class1 {
    private List<Integer> aList;

    public Class1(){
        this.aList= new ArrayList<>();
    }

    public Integer getElementAt(int i){
        return this.aList.get(i);
    }

    public int getSize(){
        return this.aList.size();
    }

    public void add(int num){
        this.aList.add(num);
    }
}

的Class2

public class Class2 {
    private Class0 c0; 
    private Class3 c3;
    public Class2(){
        this.c0 = new Class0() {
            Class1 c1 = new Class1();

            @Override
            public void abmethod1(){
                System.out.println("Abstract method 1 called. Size is " + c1.getSize());
            }

            @Override
            public void abmethod2(){
                c1.add(1);
                c1.add(2);
                c1.add(3);
                System.out.println("Abstract method 2 called. Size is " + c1.getSize());
            }
       };
       this.c3 = new Class3();
       c3.methodc2(c0);
    }
}

Class3的

public class Class3{
    public void methodc2(Class0 c0){
       c0.abmethod1();
       c0.abmethod2();
    }
}

CLASS4

public class Class4 {
    private Class2 c2;

    public Class4(){
        c2 = new Class2();
    }  
}

Class5

public class Class5 {
    private Class2 c2;

    public void method2(){
        this.c2 = new Class2();
    }
}

然后我运行主类

public class Class6 {
    public static void main(String[] afg){
        Class4 c4 = new Class4();
        Class5 c5 = new Class5();
        c5.method2();
    }
}

我收到了消息

Abstract method 1 called. Size is 0
Abstract method 2 called. Size is 3
Abstract method 1 called. Size is 0
Abstract method 2 called. Size is 3

所以每当我在Class3中调用匿名类时,我想要获取List的最后一个大小(这里是3)。如何阻止它创建Class0的新实例,以便获取输出

Abstract method 1 called. Size is 0
Abstract method 2 called. Size is 3
Abstract method 1 called. Size is 3
Abstract method 2 called. Size is 6

提前谢谢

1 个答案:

答案 0 :(得分:1)

如果您的目标是Class2的所有实例都共享Class0的同一个实例,则可以通过c0成员Class2静态实现此目标。在这种情况下,您需要在静态初始化块中而不是在构造函数中初始化c0变量。

public class Class2 {
    private static Class0 c0; 
    private Class3 c3;
    static 
    {
         Class2.c0 = new Class0() {
            Class1 c1 = new Class1();

            @Override
            public void abmethod1(){
                System.out.println("Abstract method 1 called. Size is " + c1.getSize());
            }

            @Override
            public void abmethod2(){
                c1.add(1);
                c1.add(2);
                c1.add(3);
                System.out.println("Abstract method 2 called. Size is " + c1.getSize());
            }
        };
    }
    public Class2(){
       this.c3 = new Class3();
       c3.methodc2(c0);
    }
}

如果不了解代码的目的,这一切是否有意义很难说。