Progress-4GL 中的 Final/const 关键字等效项

发布于 2024-10-27 14:32:44 字数 1458 浏览 7 评论 0原文

如果我有一个 Java 中包含不可变成员的类,我会这样做:

class MyClass {
    private final String name;
    private final int id;
    myClass(String name, int id) {
        this.name = name;
        this.id = id;
    }
    String getName() { return name; }
    int getId() { return id; }
}

在 Progress-4GL 中,您通常会看到类似这样的内容:(拜托,不要讲匈牙利表示法。我也讨厌它,但它在Progress 社区,所以这是我所接受的。)

CLASS MyClass :

    DEFINE VARIABLE mcName as CHARACTER NO-UNDO.
    DEFINE VARIABLE miId   as INTEGER   NO-UNDO.

    CONSTRUCTOR PUBLIC MyClass(INPUT ipcName AS CHARACTER,
                               INPUT ipiId AS INTEGER):
        ASSIGN mcName = ipcName
               miId   = ipiID.
    END. /* constructor(char,int)*/
END CLASS. /* MyClass */

有人告诉我,在 Progress 10.2B 中,他们添加了创建常量/最终变量的功能。但是,我无法在任何地方找到任何对它的引用。在我的 Architect(版本 10.2A)中,我确实看到 FINAL 被视为关键字。但它背后的文档却让我无法理解。

如果您曾经尝试过搜索 Progress 文档,您就会知道我的困境。

如何在 Progress 10.2B 中实现不可变变量?有什么我需要注意的问题吗?

谢谢!

编辑1我找到了关于FINAL的文档。它似乎只适用于类和方法。我目前的做法是

CLASS ImmutableString :
    DEFINE PRIVATE VARIABLE mcValue AS CHARACTER NO-UNDO.

    CONSTRUCTOR PUBLIC ImmutableString(INPUT ipcValue AS CHARACTER) :
        ASSIGN mcValue = ipcValue.
    END.

    METHOD PUBLIC CHARACTER getValue() :
        RETURN mcValue. /* Is a defensive copy required? */
    END METHOD.
END CLASS.

If I had a class with immutable members in Java, I would do this:

class MyClass {
    private final String name;
    private final int id;
    myClass(String name, int id) {
        this.name = name;
        this.id = id;
    }
    String getName() { return name; }
    int getId() { return id; }
}

In Progress-4GL, you'd typically see something like this: (Please, no lectures on Hungarian Notation. I hate it too, but it's very common in the Progress community, so it's something I just live with.)

CLASS MyClass :

    DEFINE VARIABLE mcName as CHARACTER NO-UNDO.
    DEFINE VARIABLE miId   as INTEGER   NO-UNDO.

    CONSTRUCTOR PUBLIC MyClass(INPUT ipcName AS CHARACTER,
                               INPUT ipiId AS INTEGER):
        ASSIGN mcName = ipcName
               miId   = ipiID.
    END. /* constructor(char,int)*/
END CLASS. /* MyClass */

I was told in that in Progress 10.2B, they added the ability to make constants/final variables. However, I am unable to find any reference to it anywhere. In my Architect (version 10.2A) I do see that FINAL is considered a keyword. But the documentation behind it simply eludes me.

And if you've ever tried to search for Progress documentation, you know my dilemma.

How can I do immutable variables in Progress 10.2B? Are there any gotchyas I need to be aware of?

Thanks!

EDIT 1 I found documentation on FINAL. It appears to only apply to classes and methods. My current approach is

CLASS ImmutableString :
    DEFINE PRIVATE VARIABLE mcValue AS CHARACTER NO-UNDO.

    CONSTRUCTOR PUBLIC ImmutableString(INPUT ipcValue AS CHARACTER) :
        ASSIGN mcValue = ipcValue.
    END.

    METHOD PUBLIC CHARACTER getValue() :
        RETURN mcValue. /* Is a defensive copy required? */
    END METHOD.
END CLASS.

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

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

发布评论

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

评论(1

舞袖。长 2024-11-03 14:32:44

您还可以使用公共“GET”和私有“SET”创建公共属性:

   DEF PUBLIC PROPERTY Value AS CHAR NO-UNDO
      GET.
      PRIVATE SET.

   CONSTRUCTOR PUBLIC ImmutableString(INPUT ipcValue AS CHARACTER) :
       Value = ipcValue.
   END.

这样代码会少一些,但可以完成相同的操作。

编辑以更改属性名称以匹配原始海报的示例。

You could also create a public property with a public "GET" and a private "SET":

   DEF PUBLIC PROPERTY Value AS CHAR NO-UNDO
      GET.
      PRIVATE SET.

   CONSTRUCTOR PUBLIC ImmutableString(INPUT ipcValue AS CHARACTER) :
       Value = ipcValue.
   END.

That's a little less code and does the same thing.

EDITED to change the property name to match the original poster's example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文