2

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.

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> 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.

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)

4

2 に答える 2

9

-これは、Java でタイプ セーフCollectionsを作成する方法であるため、間違ったタイプが Collection に入らないようにします。コレクションは時間内にのみチェックされ、その間はチェックされないため..つまり、Cat オブジェクトは Dog タイプの Collection に入ってはなりません。 .`CompilationRuntime

このようにできる...

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

または

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

于 2012-10-29T05:16:38.870 に答える
1

The problem is, even though Packet is-a Object, ArrayList<Packet> is not a ArrayList<Object>. Generic type is invariant in Java.

You can use the extends and super keywords with your type parameters to introduce that type of effect in Java generics.

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 Devices and return alist of Devices or take a list of some subtype of Device and return a list of the same subtype.


Side note: Coding against some concrete type (e.g ArrayList) is not a good practice. You should always code against the interface whenever possible (e.g. List).

于 2012-10-29T05:18:31.083 に答える