java - java 通用问题。为什么会这样?
问题描述
public interface IPage<T> extends Serializable {
/** @deprecated */
@Deprecated
default String[] descs() {
return null;
}
/** @deprecated */
@Deprecated
default String[] ascs() {
return null;
}
List<OrderItem> orders();
default Map<Object, Object> condition() {
return null;
}
default boolean optimizeCountSql() {
return true;
}
default boolean isSearchCount() {
return true;
}
default long offset() {
return this.getCurrent() > 0L ? (this.getCurrent() - 1L) * this.getSize() : 0L;
}
default long getPages() {
if (this.getSize() == 0L) {
return 0L;
} else {
long pages = this.getTotal() / this.getSize();
if (this.getTotal() % this.getSize() != 0L) {
++pages;
}
return pages;
}
}
default IPage<T> setPages(long pages) {
return this;
}
default void hitCount(boolean hit) {
}
default boolean isHitCount() {
return false;
}
List<T> getRecords();
IPage<T> setRecords(List<T> records);
long getTotal();
IPage<T> setTotal(long total);
long getSize();
IPage<T> setSize(long size);
long getCurrent();
IPage<T> setCurrent(long current);
default <R> IPage<R> convert(Function<? super T, ? extends R> mapper) {
List<R> collect = (List)this.getRecords().stream().map(mapper).collect(Collectors.toList());
return this.setRecords(collect);
}
default String cacheKey() {
StringBuilder key = new StringBuilder();
key.append(this.offset()).append(":").append(this.getSize());
List<OrderItem> orders = this.orders();
if (CollectionUtils.isNotEmpty(orders)) {
Iterator var3 = orders.iterator();
while(var3.hasNext()) {
OrderItem item = (OrderItem)var3.next();
key.append(":").append(item.getColumn()).append(":").append(item.isAsc());
}
}
return key.toString();
}
}
这是一个框架的源代码,当我使用convert()函数时
default <R> IPage<R> convert(Function<? super T, ? extends R> mapper) {
List<R> collect = (List)this.getRecords().stream().map(mapper).collect(Collectors.toList());
return this.setRecords(collect);
}
让我想知道的是,返回类型是新类型变量R ,他只是调用 this.setRecords(collect); 但是setRecords() 函数只接收 List < T >!
IPage<T> setRecords(List<T> records);
为了验证这一点,我自己写了一个接口,但是编译失败
public interface IPage<T> {
IPage<T> setRecords(List<T> list);
default <R> IPage<R> convert() {
List<R> collect = new ArrayList<>();
return this.setRecords(collect); //error
}
}
有人可以帮我解决我的疑惑吗?非常感谢!
解决方案
my-batis plus 的源代码包含对 IPage 的附加转换,以更改this
.
方法代码为:
default <R> IPage<R> convert(Function<? super T, ? extends R> mapper) {
List<R> collect = this.getRecords().stream().map(mapper).collect(toList());
return ((IPage<R>) this).setRecords(collect);
}
推荐阅读
- reactjs - IOS上的反应体滚动锁定问题
- shell - 从 shell 中的 json 响应中提取节点值
- python - SQLAlchemy,查询嵌套的jsonb值每次都返回空列表
- javascript - 如果用户切换到另一个窗口,异步功能是否会暂停
- django - Onchange 缺少使用 django 进行的 AJAX 验证
- android - 另一个带有复选框的回收器视图中的Android回收器视图
- javascript - ngx-charts-advanced-pie-chart 渲染没有属性或属性
- python - 从其他文件导入功能不起作用
- c - GDB 不会在断点处停止并继续
- linux - UDP 套接字和 MSG_PEEK