Array of Interface in Java

后端 未结 5 868
自闭症患者
自闭症患者 2020-12-24 09:15

I have an interface.

public interface Module {
        void init();
        void actions();
}

What happens when i try to create an array li

5条回答
  •  青春惊慌失措
    2020-12-24 10:09

    You would need to fill the array with instances of a class(es) that implement that interface.

    Module[] instances = new Module[20];
    for (int i = 0; i < 20; i++)
    {
        instances[i] = new myClassThatImplementsModule();
    }
    

提交回复
热议问题