Using generics in Java to avoid code duplication

那年仲夏 提交于 2020-02-02 06:52:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!