嵌套类,实现了接口对象

时间:2015-11-29 20:02:19

标签: java class interface nested

我想在JUnit测试用例中创建一个对象KalkulatorI.AccumulatorI。当我创建Kalkulator.Accumulator时,它就可以了(Kalkulator.Accumulator t2 = new Kalkulator().new Accumulator())。如何创建这样的接口对象?我试过了:

KalkulatorI.AccumulatorI t = new KalkulatorI.AccumulatorI();

KalkulatorI.AccumulatorI t = new KalkulatorI().new AccumulatorI();

但它不起作用。

AccumulatorI.java

public interface AccumulatorI {
    public int getValue();
    public void setValue(int acc);
}

KalkulatorI.java

public interface KalkulatorI {
    public void setAccumulatorA(AccumulatorI a);
    public void setAccumulatorB(AccumulatorI a);
}

Kalkulator.java

public class Kalkulator implements KalkulatorI {

    private AccumulatorI AA;
    private AccumulatorI BB;

    class Accumulator implements AccumulatorI{
        private int acc;

        public int getValue(){
            int res = this.acc;
            return res;
        }

        public void setValue(int acc){
            this.acc=acc;
        }
    }

    public void setAccumulatorA(AccumulatorI a){
        this.AA=a;
    }
    public void setAccumulatorB(AccumulatorI a){
        this.BB=a;
    }
}

1 个答案:

答案 0 :(得分:0)

您无法实例化界面,因此无法调用new AccumulatorI()。请查看Oracle's documentation