简体   繁体   English

Java泛型数量可变

[英]Java variable number of generics

I need a way to implement a class that can take in a variable number of generic parameters. 我需要一种方法来实现一个类,该类可以接受可变数量的通用参数。 Basically, I need a way to combine the following classes: 基本上,我需要一种组合以下类的方法:

class IncidentReporter1<A> {
    public void reportIncident(A a) {
    }
}
class IncidentReporter2<A, B> {
    public void reportIncident(A a, B b) {
    }
}
class IncidentReporter3<A, B, C> {
    public void reportIncident(A a, B b, C c) {
    }
}
class IncidentReporter4<A, B, C, D> {
    public void reportIncident(A a, B b, C c, D d) {
    }
}

into just one IncidentReporter class. 进入一个IncidentReporter类。 I know I can take in a Class[] at runtime and use that, but I was wondering if there was a better native way to do this in java. 我知道我可以在运行时使用Class[]并使用它,但是我想知道在Java中是否存在更好的本机方法。

I would probably just write it like this : 我可能会这样写:

public class IncidentReporter {

public void reportIncident(Class<?>... differentClasses) {

}

}

Much cleaner i think. 我认为干净得多。

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

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