加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Java SE 16 record 类型说明与使用

发布时间:2023-04-17 11:38:37 所属栏目:教程 来源:
导读:record 是 Java SE 16 的新特性

record 的使用场景
假设我们想创建一个不可变的类 Point,它有 x 和 y 的坐标。我们想实例化Point对象,读取它们的字段,并将它们存储在 List 中或在 Map 中作为键值使用。

record 是 Java SE 16 的新特性

record 的使用场景
假设我们想创建一个不可变的类 Point,它有 x 和 y 的坐标。我们想实例化Point对象,读取它们的字段,并将它们存储在 List 中或在 Map 中作为键值使用。

我们可以这样实现 Point 类

public class Point {
    private final int x;
    private final int y;
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Point point = (Point) o;
        if (x != point.x) return false;
        return y == point.y;
    }
    @Override
    public int hashCode() {
        int result = x;
        result = 31 * result + y;
        return result;
    }
    @Override
    public String toString() {
        return "Point{" + "x=" + x + ", y=" + y + '}';
    }
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }
}
如上代码中重复写了很多模板代码,使用 Lombok,代码可以简化成如下方式

@AllArgsConstructor
@Getter
@EqualsAndHashCode
@ToString
public class Point {
  private final int x;
  private final int y;
}
现在有了 record 上述所有代码可以简化为

public record Point(int x, int y) {}
使用javac Point.java && javap Point,我们可以查看到 Point 反编译后的结果

public final class Point extends java.lang.Record {
  public Point(int, int);
  public final java.lang.String toString();
  public final int hashCode();
  public final boolean equals(java.lang.Object);
  public int x();
  public int y();
}
和我们最初始的 Point 类定义是一样的,所以 record 可以大量简化代码的编写。

我们可以像正常使用类一样使用 record

示例代码

public class App {
    public static void main(String[] args) {
        Point p = new Point(3, 4);
        int x = p.x();
        int y = p.y();
        System.out.println(x + " " + y);
        Point p2 = new Point(3, 4);
        Point p3 = new Point(7, 5);
        System.out.println(p2.equals(p)); // 输出 true
        System.out.println(p2.equals(p3)); // 输出 false
    }
}
record 可以通过如下方式来实现多构造函数

public record Point(int x, int y) {
    public Point() {
        this(3, 3);
    }
    public Point(int v) {
        this(v, v + 3);
    }
}
record 中可以包括 static 类型变量,示例如下

public record Point(int x, int y) {
    private static final int ZERO = 0;
    private static long count = 0;
    public Point() {
        this(ZERO, ZERO);
        synchronized (Point.class) {
            count++;
        }
    }
    public static synchronized long getCount() {
        return count;
    }
    public Point(int v) {
        this(v, v + 3);
    }
}

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章