泛型中?的使用
泛型中?的作用
概念
无边界通配符?
可以直接定义变量的
Point的定义
class Point<T> {
private T x;
private T y;
public Point(){
}
public Point(T x,T y){
this.x = x;
this.y = y;
}
public void setX(T x) {
this.x = x;
}
public void setY(T y) {
this.y = y;
}
public T getX() {
return this.x;
}
public T getY() {
return this.y;
}
}
?的使用
Point<?> point;
point = new Point<Integer>(3,3);
point = new Point<Float>(4.3f,4.3f);
point = new Point<Double>(4.3d,4.90d);
point = new Point<Long>(12l,23l);
生产中的例子,用于方法传参中的类型限定
Page<T> page = slowLogService.getRecords(param);
completeAppInfo(page.getT());
private void completeAppInfo(List<? extends SlowLogRecord> records) {
records.forEach(record -> appInfoCompleter.convert(record));
}
原创:做时间的朋友