这两段代码在功能上是等价的,没有性能上的区别。在规范上,第二段代码更加规范,因为它使用了泛型,能够更好地保证类型安全。因此,我会选择第二段代码。

Mybatis Plus 源码:

public static <T> Page<T> of(long current, long size) {
    return of(current, size, 0);
}

public Page(long current, long size) {
    this(current, size, 0);
}

分析:

  • Page.of(current, size) 是一个静态方法,它返回一个新的 Page 对象。
  • new Page(current, size) 是一个构造方法,它也创建一个新的 Page 对象。

结论:

虽然两种方式都能实现分页功能,但 new Page(current, size) 由于使用了泛型,能够更好地保证类型安全,因此更加规范。在性能方面,两者没有区别。


原文地址: https://www.cveoy.top/t/topic/nHdP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录