//GeoResult 类
public class GeoResult<T> implements Serializable {
private static final long serialVersionUID = 1637452570977581370L;
private final T content; private final Distance distance;
public GeoResult(T content, Distance distance) {
Assert.notNull(content, "Content must not be null!");
Assert.notNull(distance, "Distance must not be null!");
this.content = content;
this.distance = distance;
}
}
//通过继承扩展GeoResult类
//通过继承扩展GeoResult类
public class GeoExtendResult<T> extends GeoResult<T> implements Serializable{
public GeoExtendResult(T content, Distance distance) {
super(content, distance);
}
}
//这是正确的
List<GeoResult<RedisGeoCommands.GeoLocation<String>>> geoResultList = radiusGeo.getContent();
//报类型不匹配
List<GeoExtendResult<RedisGeoCommands.GeoLocation<String>>> geoResultList = radiusGeo.getContent();
里氏替换原则了解一下
父类转子类要强转
这是一个老问题了,跟java的擦除泛型实现有关,在C#中这样写是没问题了,协变在java中需要使用子类通配符,
List<?extends GeoResult<RedisGeoCommands.GeoLocation<String>>> geoResultList = radiusGeo.getContent();
通过强转,可以成功。想在GeoExtendResult 添加自定义的属性怎么不成功?
@calm01: 添加不成功是指什么
@jello chen: 我在GeoExtendResult添加了一个test属性,在输出的json怎么没有没有?