Java中的接口数组 [英] Array of Interface in Java

查看:1450
本文介绍了Java中的接口数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个界面。

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

当我尝试创建这样的数组时会发生什么?

What happens when i try to create an array like this?

Module[] instances = new Module[20]

我如何实现这个数组?

推荐答案

是的,这是可能的。您需要使用类型为模块的对象填充数组的字段

yes, it is possible. You need to fill the fields of the array with objects of Type Module

instances [ 0] = new MyModule();

MyModule 是一个实现模块界面。或者你可以使用匿名内部类:

And MyModule is a class implementing the Module interface. Alternatively you could use anonymous inner classes:

instances[0] = new Module() {
 public void actions() {}
 public void init() {}
};

这是否回答了你的问题?

Does this answer your question?

这篇关于Java中的接口数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆