Java - 传递接口类型的 ArrayList [英] Java - passing ArrayList of interface type

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

问题描述

我有一个可损坏的接口,如下

I have an interface Damageable as follows

public interface Damageable {
    public void handleCollision(float impulse);
}

实现这个接口的类,BaseObject

a class which implements this interface, BaseObject

public class BaseObject implements Damageable

现在在第三节课中,我有一个 BaseObject 类型的 ArrayList

Now in a third class, I have an ArrayList of the type BaseObject

public class ObjectManager {    
    public ArrayList<BaseObject> bodies;

我想要做的是将 ArrayList 主体传递给另一个接受 ArrayList 的类的方法

What I am trying to do is to pass the ArrayList bodies to a method of another class which accepts ArrayList

public CollisionManager( ArrayList<Damageable> _bodies) {
        bodies = _bodies;
}

Java 不允许我执行 new CollisionManager(bodies),其中 body 的类型为 ArrayList 并且 BaseObject 实现了 Damageable

Java does not let me do new CollisionManager(bodies) where bodies is of type ArrayList and BaseObject implements Damageable

我试过铸造.说不能从ArrayList 到 ArrayList还尝试使用 Class 但是我无法调用在接口 Damageable 中声明的方法.如何传递 ArrayList?

I have tried casting. Says cannot cast from ArrayList<BaseObject> to ArrayList Also tried using Class<? extends Damageable> but then I'm unable to call methods declared in the interface Damageable. How can I pass the ArrayList?

推荐答案

你必须明确你的泛型.因此,您必须通知编译器您的泛型类型本身并不必须成为Damagable,而是可以扩展 易损:

You have to be explicit with your generics. Therefore, you have to inform the compiler that your generic type doesn't have to be a Damagable per se, rather it can extend Damagable:

public CollisionManager(ArrayList<? extends Damagable> bodies) {

<小时>

顺便说一下,请注意我将您的变量更改为 bodies 而不是 _bodies.下划线不是标准 Java 编码约定的一部分.


By the way, notice that I changed your variable to bodies rather than _bodies. Underscores are not part of the standard Java coding conventions.

根据 OP 的评论进行编辑

假设您有一个名为 Damagable具体类,而不是一个接口.告诉编译器 表示它不必是 Damagable 的实例.好的类型扩展 Damagable.否则,编译器会假定您有一个 Damagable 完全.

Let's say that, instead of an interface, you had a concrete class called Damagable. Telling the compiler <? extends Damagable> says that it doesn't have to be an instance of Damagable. It's okay that the type extend Damagable. Otherwise, the compiler assumes that you have a Damagable exactly.

当您将 Damagable 视为一个接口时,这没有多大意义,因为您不会拥有 Damagable 的实例.但它们的工作方式基本相同.

It doesn't make as much sense when you think of Damagable as an interface, since there is not case where you would have an instance of Damagable. But they work in essentially the same way.

您必须记住,您使用的是 Java 类型,而不是类.Java 的类型语法和结构不如它的类结构健壮.说到类型,没有implements的概念.

You have to remember that you're working with Java types, not classes. Java's type syntax and structure is less robust than it's class structure. There is no concept of implements when it comes to types.

最后一轮编辑

最后,我应该指出,通常最好为方法/构造函数参数和方法返回类型使用接口.这允许您和使用您的方法的人使用您喜欢的任何实现,并允许您随意更改您的实现.

Finally, I should note that it's generally better to use an interface for method/constructor parameters and method return types. This allows you and those that use your methods to use whatever implementation you please, and allows you to change your implementation as you please.

因此,通过这些修订,您将:

So with those revisions, you would have:

public CollisionManager(List<? extends Damagable> bodies) {

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

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