java中的记录类型
我如何在java中表示记录类型?
例子
TYPE Pattern = RECORD
Semantique:varchar;
type:varchar;
chemin:varchar;
END;
how can i represent a record type in java ??
exemple
TYPE Pattern = RECORD
Semantique:varchar;
type:varchar;
chemin:varchar;
END;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 JDK 14 开始 (预览功能),你可以这样写:
Starting with JDK 14 (preview feature), you can write:
如果这是一个 Bean,您可能需要 getter 和 setter。这些看起来像:
You may want getters and setters if this is going to be a bean. Those would look like:
您可以将其粗略地翻译为 Java,如下所示:
但是,这在细节上可能与您要翻译的任何语言(Pascal?)有很大不同。例如,您无法直接读取 Java 类的实例并将其写入磁盘文件。
You can roughly translate that to Java like this:
However, this is likely to be substantially different in details than whatever language you're translating from (Pascal?). For example, you can't read and write instances of Java classes directly to disk files.
Java 表示(从 JDK 17 开始)是:
Java 记录是不可变的,即。 e.它的成员不能改变,而且它们是私有的。也就是说,它们的引用不能更改,它们的状态可以,除非它们是 String、LocalDate 等不可变类的实例。
通过此(无需添加任何更多代码),您将获得:
a) 规范构造函数
b) 读取访问器
c) A
boolean equals(Object other)
这是基于成员的相等性——至少对我来说它的行为是这样的。
在经典 Java 中,上面的一句话看起来像这样:
我不确定
.hashCode()
是否也基于成员的质量,但我希望它是。您可以添加:
输入验证
自定义构造函数
这需要委托给另一个构造函数,最终调用链末尾的规范构造函数。
自定义方法
从您所在域的角度来看,这可能很愚蠢,但它展示了技术上的可能性。
The Java representation (as of JDK 17) would be:
A Java record is immutable, i. e. its members cannot be altered plus they are private. That is, their references cannot be altered, their states can unless they are instances of immutable classes like String, LocalDate etc.
By this—without adding any more code—you will get:
a) A canonical constructor
b) Reading accessors
c) A
boolean equals(Object other)
This is based on equality of the members—at least it behaves like that for me.
In classical Java the above one-liner would look like this:
I am not sure, wether the
.hashCode()
is based on quality of the members, too, but I would expect it to be.You can add:
validation of input
custom constructors
This one needs to delegate to another constructor, ultimately calling the canonical constructor at the end of the chain.
custom methods
This might be silly from your domain's perspective, but it demonstrates the technical possibility.