简体   繁体   English

Java中有没有办法交出HashSet <Class> 方法的参数,以便该方法将接受Class子类的HashSets?

[英]Is there a way in Java to hand over HashSet<Class> arguments to a method so that the method will accept HashSets of subclasses of Class?

Question & Explanation 问题与解释

Is there a way in Java to hand over HashSet<Class> arguments to a method so that the method will accept HashSets of subclasses of Class ? 有没有在Java中的方式来交出HashSet<Class>参数的方法,使该方法将接受的子类的HashSets Class

This would be useful if there are several subclasses of a class the differences of which are not relevant to the method in question. 如果某个类的多个子类的区别与所讨论的方法无关,则这将很有用。 If this is not possible, the only options to handle this seem to be 如果这不可能,那么解决此问题的唯一选择似乎是

  • to write as many methods as there are subclasses 编写与子类一样多的方法
  • to loop through the HashSet in the calling function and call a similar method with the individual class instances as argument instead of the HashSets 遍历调用函数中的HashSet并以单个类实例作为参数而不是HashSets调用类似的方法

... both are not very practical. ...两者都不是很实用。

The version is Java 8 (openjdk 1.8.0). 版本是Java 8(openjdk 1.8.0)。

Example

All files are assumed to be in the same directory so that we do not have to worry about packages and files importing each other. 假定所有文件都在同一目录中,因此我们不必担心包和文件相互导入。 Consider a classes NNB and a subclass NNC (subclass of NNB). 考虑一个NNB类和一个NNC子类(NNB的子类)。

NNB.java : NNB.java

import java.util.HashMap;
import java.util.Map;


public class NNB {

    public final Map<Long, Double> dict;

    public NNB () {
        this.dict = new HashMap<Long, Double>();
        int c = 0;
        while (c<10) {
            Long XX = (long)10;
            Double YY = 2.0;
            this.dict.merge(XX, YY, Double::sum); 
            System.out.println(dict);
            c++;
        }
    }
}

NNC.java : NNC.java

import java.util.HashMap;
import java.util.Map;


public class NNC extends NNB {

    private Map<Long, Double> dict2;

    public NNC () {
        super();
        this.dict2 = new HashMap<Long, Double>();
    }
}

Now assume, we want to call a method over a list of instances of NNB and NNC (say, to print NNB.dict and NNC.dict ). 现在假设,我们要在NNB和NNC实例列表上调用一个方法(例如,打印NNB.dictNNC.dict )。

This does not work: NNA2.java : 这不起作用: NNA2.java

import java.util.HashSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

public class NNA2 {

    public void printDict(HashSet<NNB> nnbs) {
        for (NNB nnb : nnbs) {
            System.out.println(nnb.dict);
        }
    }

    public static void main(String args[]) {
        NNA2 inst = new NNA2();
        HashSet<NNB> x = new HashSet<NNB>(Arrays.asList(new NNB()));
        HashSet<NNC> y = new HashSet<NNC>(Arrays.asList(new NNC()));

        inst.printDict(x);
        inst.printDict(y); //here it fails
        System.exit(0);
    }
}

Error: 错误:

NNA2.java:26: error: incompatible types: HashSet<NNC> cannot be converted to HashSet<NNB>
        inst.printDict(y);

This works (but is not quite what we wanted and would be unpractical if we wanted to perform more complex operations): NNA1.java : 这可以工作(但不是我们想要的,如果想要执行更复杂的操作,这将不切实际): NNA1.java

import java.util.HashSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

public class NNA1 {

    public void printOneDict(NNB nnb) {
        System.out.println(nnb.dict);
    }

    public static void main(String args[]) {
        NNA1 inst = new NNA1();
        HashSet<NNB> x = new HashSet<NNB>(Arrays.asList(new NNB()));
        HashSet<NNC> y = new HashSet<NNC>(Arrays.asList(new NNC()));

        for (NNB nnb : x) {
            inst.printOneDict(nnb);
        }

        for (NNC nnb : y) {
            inst.printOneDict(nnb);
        }

        System.exit(0);
    }
}

If I understand you correctly, you're looking for this: 如果我对您的理解正确,那么您正在寻找以下内容:

public void printDict(HashSet<? extends NNB> nnbs) {
    // ...
}

See also Is List<Dog> a subclass of List<Animal>? 另请参见List <Dog>是List <Animal>的子类吗? Why aren't Java's generics implicitly polymorphic? 为什么不是Java的泛型隐含多态? and What is PECS (Producer Extends Consumer Super)? 什么是PECS(生产商扩展了超级消费者)?

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

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