java中的记录类型

发布于 2024-11-01 15:50:18 字数 204 浏览 3 评论 0原文

我如何在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ぺ禁宫浮华殁 2024-11-08 15:50:18

JDK 14 开始 (预览功能),你可以这样写:

record Pattern (String semantique, String type, String chemin);

Starting with JDK 14 (preview feature), you can write:

record Pattern (String semantique, String type, String chemin);
誰認得朕 2024-11-08 15:50:18
public class Record {
  String semantique;
  String type;
  String chemin;
}

如果这是一个 Bean,您可能需要 getter 和 setter。这些看起来像:

public void setType(String type){ this.type=type; }

public String getType() { return type; }
public class Record {
  String semantique;
  String type;
  String chemin;
}

You may want getters and setters if this is going to be a bean. Those would look like:

public void setType(String type){ this.type=type; }

public String getType() { return type; }
夏日落 2024-11-08 15:50:18

您可以将其粗略地翻译为 Java,如下所示:

public class Pattern {
    public String Semantique;
    public String type;
    public String chemin;
}

但是,这在细节上可能与您要翻译的任何语言(Pascal?)有很大不同。例如,您无法直接读取 Java 类的实例并将其写入磁盘文件。

You can roughly translate that to Java like this:

public class Pattern {
    public String Semantique;
    public String type;
    public String chemin;
}

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.

东风软 2024-11-08 15:50:18

Java 表示(从 JDK 17 开始)是:

record Pattern(String semantique, String type; String chemin) { };

Java 记录是不可变的,即。 e.它的成员不能改变,而且它们是私有的。也就是说,它们的引用不能更改,它们的状态可以,除非它们是 String、LocalDate 等不可变类的实例。

通过此(无需添加任何更多代码),您将获得:

a) 规范构造函数

public Pattern(String semantique, String type, String chemin) {
  this.semantique = semantique;
  this.type = type;
  this.chemin = chemin;
}

b) 读取访问器

public String semantique();
public String type();
public String chemin();

c) A boolean equals(Object other)

这是基于成员的相等性——至少对我来说它的行为是这样的。


在经典 Java 中,上面的一句话看起来像这样:

public class Pattern {
  private final String semantique;
  private final String type;
  private final String chemin;

  public Pattern(String semantique; String type; String chemin) {
    this.semantique = semantique;
    this.type = type;
    this.chemin = chemin;
  }

  public String semantique() {
    return semantique;
  }

  public String type() {
    return type;
  }

  public String chemin() {
    return chemin;
  }

  public equals(Object other) {
    if (other == null)
      return false;

    if (false == Pattern.class.isInstance(other)
      return false;

    final var typesafeOther = (Pattern) other;
    return Objects.equals(this.semantique, typeSafeOther.semantique)
      && Objects.equals(this.type, typeSafeOther.type)
      && Objects.equals(this.chemin, typeSafeOther.chemin);
  }
}

我不确定 .hashCode() 是否也基于成员的质量,但我希望它是。


您可以添加:

输入验证

Pattern {
  Objects.requireNonNull(semantique);
  Objects.requireNonNull(type);
  Objects.requireNonNull(chemin);
}

自定义构造函数

这需要委托给另一个构造函数,最终调用链末尾的规范构造函数。

public Pattern() {
  this("n/A", "n/A", "n/A");
}

自定义方法

从您所在域的角度来看,这可能很愚蠢,但它展示了技术上的可能性。

public boolean hasSemantique() {
  return semantique != null && false == semantique.trim().length() > 0;
}

The Java representation (as of JDK 17) would be:

record Pattern(String semantique, String type; String chemin) { };

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

public Pattern(String semantique, String type, String chemin) {
  this.semantique = semantique;
  this.type = type;
  this.chemin = chemin;
}

b) Reading accessors

public String semantique();
public String type();
public String chemin();

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:

public class Pattern {
  private final String semantique;
  private final String type;
  private final String chemin;

  public Pattern(String semantique; String type; String chemin) {
    this.semantique = semantique;
    this.type = type;
    this.chemin = chemin;
  }

  public String semantique() {
    return semantique;
  }

  public String type() {
    return type;
  }

  public String chemin() {
    return chemin;
  }

  public equals(Object other) {
    if (other == null)
      return false;

    if (false == Pattern.class.isInstance(other)
      return false;

    final var typesafeOther = (Pattern) other;
    return Objects.equals(this.semantique, typeSafeOther.semantique)
      && Objects.equals(this.type, typeSafeOther.type)
      && Objects.equals(this.chemin, typeSafeOther.chemin);
  }
}

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

Pattern {
  Objects.requireNonNull(semantique);
  Objects.requireNonNull(type);
  Objects.requireNonNull(chemin);
}

custom constructors

This one needs to delegate to another constructor, ultimately calling the canonical constructor at the end of the chain.

public Pattern() {
  this("n/A", "n/A", "n/A");
}

custom methods

This might be silly from your domain's perspective, but it demonstrates the technical possibility.

public boolean hasSemantique() {
  return semantique != null && false == semantique.trim().length() > 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文