泛型的具体应用-小羊的记录本

泛型的具体应用

  • 解决的问题:
    在不同的业务调用API接口的时候,可能传参的类型不同,但是一般来说是基于一个父类的一系列类型。比如:Cell是基本的类,有三个参数(cgi,lat,lon),业务可能想传入5GCell(cgi,lat,lon,others),那么如何进行泛型的应用?使得接口更加公共化?
    在这里插入图片描述

1.基类的接口【CellGeo】:getCgi(),getLon(),getLat()

public interface CellGeo {

	public String getCgi();
	public double getLon();
	public double getLat();

	public boolean equals(Object obj);
    public int hashCode();
}

2.基类【CellGeoInfo】

public class CellGeoInfo {

    private String cgi;
    private double lon;
    private double lat;

    public CellGeoInfo(String cgi, double lon, double lat) {
        this.cgi = cgi;
        this.lon = lon;
        this.lat = lat;
    }

    public String getCgi() {	return cgi;}
    public double getLon() {	return lon;}
    public double getLat() {	return lat;}

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        CellGeoInfo that = (CellGeoInfo) obj;
        return Objects.equals(cgi, that.cgi);
    }

    @Override
    public int hashCode() {
        return Objects.hash(cgi);
    }
}

3.任意类(实现基类的接口)【YangCell】

public class YangCell implements CellGeo {

    private String cgi;
    private double lon;
    private double lat;
    private double other;

    public YangCell(String cgi, double lon, double lat, double other) {
        this.cgi = cgi;
        this.lon = lon;
        this.lat = lat;
        this.other = other;
    }

    @Override
    public String getCgi() {
        return cgi;
    }
    @Override
    public double getLon() {
        return lon;
    }
    @Override
    public double getLat() {
        return lat;
    }

    public double getOther() {
        return other;
    }

}

4.API接口【TwoDimensionalGrid】

public class TwoDimensionalGrid {

    private static final int MAX_GRID_NUM = 10000;
    private static final double LONGITUDE_STEP = 0.03;
    private static final double LATITUDE_STEP = 0.03;

    private int maxGridNum = MAX_GRID_NUM;
    private double longitudeStep = LONGITUDE_STEP;
    private double latitudeStep = LATITUDE_STEP;

    public static <T extends CellGeo> TwoDimensionalGrid newInstance(List<T> cellList) {
        List<CellGeoInfo> res = transfor(cellList);
        return new TwoDimensionalGrid(res);
    }
    public TwoDimensionalGrid setMaxGridNum(int gridNum) {
        this.maxGridNum = gridNum;
        return this;
    }
    public static <T extends CellGeo> List<CellGeoInfo> transfor(List<T> cellList) {

        List<CellGeoInfo> cellGeoInfoList = new ArrayList<>();
        Type genericType = cellList.get(0).getClass();

        for (T cell5: cellList) {
            CellGeoInfo cell4 =new CellGeoInfo(cell5.getCgi(),cell5.getLon(),cell5.getLat());
            cellGeoInfoList.add(cell4);
        }
        System.out.println(cellGeoInfoList.get(0).getClass());
        return cellGeoInfoList;
    }

    public TwoDimensionalGrid setLongitudeStep(double lonStep) {
        this.longitudeStep = lonStep;
        return this;
    }

    public TwoDimensionalGrid setLatitudeStep(double latStep) {
        this.latitudeStep = latStep;
        return this;
    }
    private TwoDimensionalGrid(List<CellGeoInfo> cellList) {
        if (cellList != null && !cellList.isEmpty()) {
            for (CellGeoInfo i:cellList) {
                System.out.println(i.getCgi());
                System.out.println("["+i.getCgi()+","+i.getLat()+","+i.getLon()+"]");
            }

        }
    }
}

5.业务调用【Test】

public class Test {
    public static void main(String[] args) {
        List<YangCell> cellGeoInfoList55 = Arrays.asList(
            new YangCell("张三", 18, 10000.11,56.55),
            new YangCell("李四", 50, 50000.55,456.05),
            new YangCell("王五", 70, 2000.22,56.99)
        );
        TwoDimensionalGrid twoDimensionalGrid = TwoDimensionalGrid.newInstance(cellGeoInfoList55).setMaxGridNum(10000);
    }
}

©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页