有没有一种方法可以在继承中使用记录(Records)?

huangapple go评论73阅读模式
英文:

Is there any way of using Records with inheritance?

问题

我有一堆使用Lombok的@Data类,我想将它们全部迁移到Java 14中可用的新Record功能。

我知道现在可能还有点早,但这是我正在进行的一个实验性测试。

这里的主要问题涉及继承。我有一个类B,它扩展了类A。有没有办法在继承中使用Records?

英文:

I have a bunch of @Data classes using Lombok and I want to migrate all of them to use the new Record functionality available in Java 14.

I know it's a little bit earlier but this is an experimental test that I'm doing.

The main problem here is involving inheritance. I have a class B which extends a class A. Is there any way of using Records with inheritance?

答案1

得分: 45

以下是翻译好的部分:

这个JEP阐述了:

> 关于记录的限制
> ---------------------------
>
> 记录不能扩展任何其他类,也不能声明除了与状态描述的组件相对应的私有final字段之外的实例字段。任何其他声明的字段都必须是静态的。这些限制确保只有状态描述本身定义了表示。

Java 17 JLS 8.10 注意<sup>1</sup>:

> record声明没有extends从句,因此不可能明确声明直接的超类类型,甚至是Record

然而,record 可以 implement接口,因此可以在多态情况下使用它们。此外,由于记录将继承它们实现的接口中的任何default方法,它们支持有限形式的继承。


<sup>1 - 这是一个非规范性的陈述,但显然是真实的,因为RecordDeclaration的指定语法明确不允许使用extends。</sup>

英文:

The JEP states this:

> Restrictions on records
> ---------------------------
>
> Records cannot extend any other class, and cannot declare instance fields other than the private final fields which correspond to components of the state description. Any other fields which are declared must be static. These restrictions ensure that the state description alone defines the representation.

The Java 17 JLS 8.10 notes<sup>1</sup> this:

> A record declaration does not have an extends clause, so it is not possible to explicitly declare a direct superclass type, even Record.

However, a record can implement interfaces, so you can use them polymorphically. Furthermore, since records will inherit any default methods in the interfaces that they implement, they do support a limited form of inheritance.


<sup>1 - This is a non-normative statement, but it is obviously true since the specified syntax for RecordDeclaration clearly does not allow extends to be used.</sup>

答案2

得分: 36

Records已经扩展了 java.lang.Record。由于Java不允许多重继承,records无法扩展任何其他类。

例如,考虑以下记录 Point

public record Point(double x, double y) {}

您可以使用以下命令进行编译:

javac --enable-preview -source 14 Point.java

借助 javap,您可以查看有关生成的 Point 代码的详细信息:

javap -p Point

输出将会是:

Compiled from "Point.java"
public final class Point extends java.lang.Record {
  private final double x;
  private final double y;
  public Point(double, double);
  public java.lang.String toString();
  public final int hashCode();
  public final boolean equals(java.lang.Object);
  public double x();
  public double y();
}
英文:

> Is there any way of using records with inheritance?

Records already extend java.lang.Record. As Java does not allow multiple inheritance, records cannot extend any other class.

Consider, for example, the following record Point:

public record Point(double x, double y) {}

You can compile it using:

javac --enable-preview -source 14 Point.java

With the help of javap, you can can have details about the code generate for Point:

javap -p Point

The output will be:

Compiled from &quot;Point.java&quot;
public final class Point extends java.lang.Record {
  private final double x;
  private final double y;
  public Point(double, double);
  public java.lang.String toString();
  public final int hashCode();
  public final boolean equals(java.lang.Object);
  public double x();
  public double y();
}

huangapple
  • 本文由 发表于 2020年8月27日 05:11:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63605794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定