aka*_*101 7 java generics class
我需要一种方法来实现一个可以接受可变数量的泛型参数的类。基本上,我需要一种方法来组合以下类:
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) {
}
}
Run Code Online (Sandbox Code Playgroud)
只进一个IncidentReporter班。我知道我可以Class[]在运行时接收并使用它,但我想知道在 java 中是否有更好的本地方法来做到这一点。
小智 1
我可能会这样写:
public class IncidentReporter {
public void reportIncident(Class<?>... differentClasses) {
}
}
Run Code Online (Sandbox Code Playgroud)
我认为更干净。