Java教程

11 | JAVA纪录类Record

本文主要是介绍11 | JAVA纪录类Record,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

纪录类 (也是一种不变类)

使用StringInteger等类型的时候,这些类型都是不变类,一个不变类具有以下特点:

  1. 定义class时使用final,无法派生子类;
  2. 每个字段使用final,保证创建实例后无法修改任何字段。

假设我们希望定义一个Point类,有xy两个变量,同时它是一个不变类,可以这么写:

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不变类的写法目的是,这些代码写起来都非常简单,但是很繁琐。

从Java 14开始,引入了新的Record类。我们定义Record类时,使用关键字record。把上述Point类改写为Record类,代码如下:

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) {}

把上述定义改写为class,相当于以下代码:

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() {
        ...
    }
}

换句话说,使用record关键字,可以一行写出一个不变类。

补充构造函数

假设Point类的xy不允许负数,我们就得给Point的构造方法加上检查逻辑:

public record Point(int x, int y) {
    public Point {
        if (x < 0 || y < 0) {
            throw new IllegalArgumentException();
        }
    }
}

注意到方法public Point {...}被称为Compact Constructor,它的目的是让我们编写检查逻辑,编译器最终生成的构造方法如下:

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

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);
    }
}

这样我们可以写出更简洁的代码: (不同调用new Point了)(不同调用new Point了)(不同调用new Point了)

var z = Point.of();
var p = Point.of(123, 456);
这篇关于11 | JAVA纪录类Record的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!