Java 记录类

本文最后更新于:2024年3月18日 凌晨

Java 记录类

  • 使用String,Integer等类型的时候,这些类型都是不变类,一个不变类具有以下特点:
    1. 定义class时使用final,无法派生子类。
    2. 每个字段使用final,保证创建实例后无法修改任何字段。
  • 假设我们希望定义一个Point类,有x,y两个变量,同时它是一个不变类,可以这么写:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public final class Point {
private final int x;
private final int y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}

public int x() {
return this.x;
}

public int y() {
return this.y;
}
}
  • 为了保证不变类的比较,还需要正确覆写equals()hashCode()方法,这样才能在集合类中正常使用,后续我们会详细讲解正确覆写equals()hashCode(),这里演示Point不变类的写法目的是,这些代码写起来都非常简单,但是很繁琐。

record

  • 从Java 14开始,引入了新的Record类,我们定义Record类时,使用关键字record,把上述Point类改写为Record类,代码如下:
1
2
3
4
5
6
7
8
9
10
public class Main {
public static void main(String[] args) {
Point p = new Point(123, 456);
System.out.println(p.x());
System.out.println(p.y());
System.out.println(p);
}
}

public record Point(int x, int y) {}
  • 仔细观察Point的定义:
1
public record Point(int x, int y) {}
  • 把上述定义改写为class,相当于以下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public final class Point extends Record {
private final int x;
private final int y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}

public int x() {
return this.x;
}

public int y() {
return this.y;
}

public String toString() {
return String.format("Point[x=%s, y=%s]", x, y);
}

public boolean equals(Object o) {
...
}
public int hashCode() {
...
}
}
  • 除了用final修饰class以及每个字段外,编译器还自动为我们创建了构造方法,和字段名同名的方法,以及覆写toString(),equals()hashCode()方法。
  • 换句话说,使用record关键字,可以一行写出一个不变类。
  • enum类似,我们自己不能直接从Record派生,只能通过record关键字由编译器实现继承。

构造方法

  • 编译器默认按照record声明的变量顺序自动创建一个构造方法,并在方法内给字段赋值,那么问题来了,如果我们要检查参数,应该怎么办?
  • 假设Point类的x,y不允许负数,我们就得给Point的构造方法加上检查逻辑:
1
2
3
4
5
6
7
public record Point(int x, int y) {
public Point {
if (x < 0 || y < 0) {
throw new IllegalArgumentException();
}
}
}
  • 注意到方法public Point {...}被称为Compact Constructor,它的目的是让我们编写检查逻辑,编译器最终生成的构造方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
public final class Point extends Record {
public Point(int x, int y) {
// 这是我们编写的Compact Constructor:
if (x < 0 || y < 0) {
throw new IllegalArgumentException();
}
// 这是编译器继续生成的赋值代码:
this.x = x;
this.y = y;
}
...
}
  • 作为recordPoint仍然可以添加静态方法,一种常用的静态方法是of()方法,用来创建Point:
1
2
3
4
5
6
7
8
public record Point(int x, int y) {
public static Point of() {
return new Point(0, 0);
}
public static Point of(int x, int y) {
return new Point(x, y);
}
}
  • 这样我们可以写出更简洁的代码:
1
2
var z = Point.of();
var p = Point.of(123, 456);

小结

从Java 14开始,提供新的record关键字,可以非常方便地定义Data Class:

  • 使用record定义的是不变类。
  • 可以编写Compact Constructor对参数进行验证。
  • 可以定义静态方法。

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!