Vala - 对象构造函数中的常量初始化和赋值

发布于 2024-12-12 02:12:11 字数 531 浏览 0 评论 0原文

过去几天我一直在研究 Vala 编程语言,它看起来很有前途。但是,我不知道如何在对象构造中正确分配常量(目前 Vala 相当于 final)。例如,在Java中:

import java.lang.Math;

public class Rectangle {
    public final double sideA;
    public final double sideB;
    public final double area;
    public final double diagonal;

    public Rectangle (double SideA, double SideB) {
        sideA = SideA;
        sideB = SideB;
        area = SideA * SideB;
        diagonal = Math.sqrt(Math.pow(SideA, 2) + Math.pow(SideB, 2));
    }
}

这在Vala中该怎么写?

I've been looking at the Vala programming language over the past few days, and it looks promising. However, I can't figure out how to properly assign a constant (currently the Vala equivalent to final) in object construction. For example, in Java:

import java.lang.Math;

public class Rectangle {
    public final double sideA;
    public final double sideB;
    public final double area;
    public final double diagonal;

    public Rectangle (double SideA, double SideB) {
        sideA = SideA;
        sideB = SideB;
        area = SideA * SideB;
        diagonal = Math.sqrt(Math.pow(SideA, 2) + Math.pow(SideB, 2));
    }
}

How would this be written in Vala?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

黒涩兲箜 2024-12-19 02:12:11

Vala 没有与 Java 的 Final 关键字直接等效的关键字。我认为您能得到的最接近的结果是这样的:

public class Rectangle : GLib.Object {
  public double sideA { get; construct; }
  public double sideB { get; construct; }
  public double area { get; construct; }
  public double diagonal { get; construct; }

  public Rectangle (double SideA, double SideB) {
    GLib.Object (
      sideA: SideA,
      sideB: SideB,
      area: SideA * SideB,
      diagonal: Math.sqrt(Math.pow(SideA, 2) + Math.pow(SideB, 2)));
  }
}

构造属性与最终属性有点不同,很大程度上是因为 GObject 构造的工作方式。它们只能在构造时设置,但与Java中的final不同(IIRC...我的大部分Java知识都被压抑了)它们也可以在子类构造期间设置。例如,这是完全可以接受的:

public class Square : Rectangle {
  public Square (double Side) {
    GLib.Object (
      sideA: Side,
      sideB: Side,
      area: Side * Side,
      diagonal: Math.sqrt(Math.pow(Side, 2) + Math.pow(Side, 2)));
  }
}

所以,如果您想允许 GObject 风格的构造(如果您正在制作一个其他人会调用的库,我建议您这样做......如果代码只适合您,则不需要),你可能想做更多类似这样的事情:

public class Rectangle : GLib.Object {
  public double sideA { get; construct; }
  public double sideB { get; construct; }

  private double? _area = null;
  public double area {
    get {
      if ( _area == null )
        _area = sideA * sideB;

      return _area;
    }
  }

  private double? _diagonal = null;
  public double diagonal {
    get {
      if ( _diagonal == null )
        _diagonal = Math.sqrt(Math.pow(sideA, 2) + Math.pow(sideB, 2));

      return _diagonal;
    }
  }

  public Rectangle (double SideA, double SideB) {
    GLib.Object (
      sideA: SideA,
      sideB: SideB,
      area: SideA * SideB,
      diagonal: Math.sqrt(Math.pow(SideA, 2) + Math.pow(SideB, 2)));
  }
}

Vala doesn't have a direct equivalent of Java's final keyword. I think the closest you are going to be able to come is something like this:

public class Rectangle : GLib.Object {
  public double sideA { get; construct; }
  public double sideB { get; construct; }
  public double area { get; construct; }
  public double diagonal { get; construct; }

  public Rectangle (double SideA, double SideB) {
    GLib.Object (
      sideA: SideA,
      sideB: SideB,
      area: SideA * SideB,
      diagonal: Math.sqrt(Math.pow(SideA, 2) + Math.pow(SideB, 2)));
  }
}

construct properties are a bit different from final, largely because of how GObject construction works. They can only be set at construct time, but unlike final in Java (IIRC... most of my Java knowledge has been repressed) they can also be set during construct by a subclass. For example, this is perfectly acceptable:

public class Square : Rectangle {
  public Square (double Side) {
    GLib.Object (
      sideA: Side,
      sideB: Side,
      area: Side * Side,
      diagonal: Math.sqrt(Math.pow(Side, 2) + Math.pow(Side, 2)));
  }
}

So, if you want to allow GObject-style construction (which I would suggest you do if you are making a library other people will call... if the code is just for you there is no need), you might want to do something more like this:

public class Rectangle : GLib.Object {
  public double sideA { get; construct; }
  public double sideB { get; construct; }

  private double? _area = null;
  public double area {
    get {
      if ( _area == null )
        _area = sideA * sideB;

      return _area;
    }
  }

  private double? _diagonal = null;
  public double diagonal {
    get {
      if ( _diagonal == null )
        _diagonal = Math.sqrt(Math.pow(sideA, 2) + Math.pow(sideB, 2));

      return _diagonal;
    }
  }

  public Rectangle (double SideA, double SideB) {
    GLib.Object (
      sideA: SideA,
      sideB: SideB,
      area: SideA * SideB,
      diagonal: Math.sqrt(Math.pow(SideA, 2) + Math.pow(SideB, 2)));
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文