Progress-4GL 中的 Final/const 关键字等效项
如果我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还可以使用公共“GET”和私有“SET”创建公共属性:
这样代码会少一些,但可以完成相同的操作。
编辑以更改属性名称以匹配原始海报的示例。
You could also create a public property with a public "GET" and a private "SET":
That's a little less code and does the same thing.
EDITED to change the property name to match the original poster's example.