问题
I've defined two classes:
public class GreffonTramway extends Tramway {
private ServiceCollecte collecte;
public GreffonTramway(int nbPlaceAssise, int nbPlaceDebout, ServiceCollecte coll){
super(nbPlaceAssise, nbPlaceDebout);
collecte = coll;
}
...
}
public class GreffonAutobus extends Autobus {
private ServiceCollecte collecte;
public GreffonAutobus(int nbPlaceAssise, int nbPlaceDebout, ServiceCollecte coll){
super(nbPlaceAssise, nbPlaceDebout);
collecte = coll;
}
...
}
The only difference between them is the name and the class they extend. Would something like this be allowed in Java, so as to have a single file from which we can create both classes?
public class Greffon<T> extends <T> {
...
public Greffon<T>(...){}
}
If not, how could we achieve this?
来源:https://stackoverflow.com/questions/59017583/using-generics-in-java-to-avoid-code-duplication