简体   繁体   English

如何使方法参数类型为ArrayList <Object> 采取不同的对象类型

[英]how to make method parameter type ArrayList<Object> take different object types

I have an abstract method as part of an abstract class with the following declaration: 我有一个抽象方法作为抽象类的一部分,具有以下声明:

abstract public ArrayList<Device> returnDevices(ArrayList<Object> scanResult);

I want the parameter that is passed to be an ArrayList, but the type object in the ArrayList will be dependent on the child class that inherits this superclass and implements the method returnDevices. 我希望传递的参数是一个ArrayList,但ArrayList中的类型对象将依赖于继承此超类并实现方法returnDevices的子类。

I thought that I could achieve this by making the method abstract as above, and then in the child class that inherits it do something like: 我认为我可以通过如上所述使方法抽象来实现这一点,然后在继承它的子类中执行以下操作:

public ArrayList<Device> returnDevices(ArrayList<Object> scanResult) {

    Iterator<Object> results = scanResult.iterator();
    while(results.hasNext())
        Packet pkt = (Packet) results.next();  // HERE: I cast the Object
}

That is fine and does not cause an error, but when I try to call returnDevices by using a parameter of type ArrayList<Packet> , like the following: 这很好,不会导致错误,但是当我尝试使用ArrayList<Packet>类型的参数调用returnDevices时,如下所示:

ArrayList<Packet> packets = new ArrayList<Packet>();
// <----- the "packets" ArrayList is filled here
ArrayList<Device> devices = returnDevices(packets);

... I get the error: ......我收到错误:

The method returnDevices(ArrayList<Object>) in the type ScanResultParser is not applicable for the arguments (ArrayList<Packet>)

So clearly it is rejecting the parameter type. 很明显它拒绝参数类型。 What is the proper way to achieve what I am trying to do? 实现我想做的事情的正确方法是什么?

- This is how Collections are made type-safed in Java so a wrong type doesn't enter into the Collection , As collection are checked only during the `Compilation time and Not during the Runtime .. ie a Cat object should not enter into a Collection of type Dog. -这就是如何在Java中对Collections进行类型安全处理 ,因此错误的类型不会进入集合 ,因为集合`Compilation时”和“ Runtime期间进行检查,即Cat对象不应进入类型狗的集合。

You can do it this way... 你可以这样做......

public ArrayList<Device> returnDevices(ArrayList<? extends Object> scanResult)

Or 要么

public <T extends Object> ArrayList<Device> returnDevices(ArrayList<T> scanResult)

The problem is, even though Packet is-a Object , ArrayList<Packet> is not a ArrayList<Object> . 问题是,即使Packet是一个ObjectArrayList<Packet>也不是ArrayList<Object> Generic type is invariant in Java . 通用类型在Java中是不变的

You can use the extends and super keywords with your type parameters to introduce that type of effect in Java generics. 您可以将extendssuper关键字与您的类型参数一起使用,以在Java泛型中引入该类型的效果。

I think the following is what it should be 我认为以下是应该的

abstract public <T extends Device> List<T> returnDevices(List<T> scanResult);

With that the method with either take a list of Device s and return alist of Device s or take a list of some subtype of Device and return a list of the same subtype. 随着与任一方法采取的列表Device S和返回ALIST Device S或采取的一些亚型的列表Device并返回相同亚型的列表。


Side note: Coding against some concrete type (eg ArrayList ) is not a good practice. 旁注:针对某些具体类型(例如ArrayList )进行编码不是一个好习惯。 You should always code against the interface whenever possible (eg List ). 您应该尽可能对接口进行编码(例如List )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM