本文最后更新于:2024年3月18日 凌晨
Java 记录类
使用String
,Integer
等类型的时候,这些类型都是不变类,一个不变类具有以下特点:
定义class时使用final
,无法派生子类。
每个字段使用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) {}
1 public record Point (int x, int y) {}
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) { if (x < 0 || y < 0 ) { throw new IllegalArgumentException(); } this .x = x; this .y = y; } ... }
作为record
的Point
仍然可以添加静态方法,一种常用的静态方法是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对参数进行验证。
可以定义静态方法。