scala 中的私有字段设置器
让 scala 中的类字段在类之外只读的最佳方法是什么?我意识到我可以做这样的事情:
private var myvalX = 0 // default val
def myval = myvalX
def myval_=(x: Int) { myvalX = x }
但我发现它非常丑陋,因为 _= 运算符以及您需要定义一个单独的 var ,其名称与方法不同。替代方案将非常受欢迎。谢谢你!
what is the best way to have a class field in scala be read-only outside of the class? i realize i can do something like this:
private var myvalX = 0 // default val
def myval = myvalX
def myval_=(x: Int) { myvalX = x }
but i find it extremely ugly due to the _= operator and the fact that you need to define a separate var whose name is different than the methods. alternatives would be very welcome. thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为私有字段和公共字段同名并不是一个好的做法。例如,您编写的
内容根本不会使
myval
成为只读!您的意思可能是只读的。但这是一个完美的例子,说明了为什么这是一个坏主意——您要求混淆公共接口和私有实现细节。可能出现的其他问题是用户没有意识到值可能会从他们的下面改变,子类重新定义公共 getter 而没有意识到私有 setter 不可用,等等。
如果您不尝试共享名称,那么很明显需要考虑两件事:您的基础数据以及依赖于该数据的公共接口。
那还不错,不是吗?
或者,如果您确实坚持使用这种模式,则可以使用各种技巧让事情看起来更好一些。例如,如果您有一堆字段,您可以
让 C 类中的任何内容设置
data.value
和comment.value
,并且只让其他人读取它们。 (编辑:修改为包含原始示例中的所有好东西,因为如果省略它们,您可能会出错。)I don't think it's good practice to have private fields and public ones share the same name. For example, you wrote
which doesn't make
myval
read-only at all! You probably meant something likewhich would be read-only. But this is a perfect example of why it's a bad idea--you are asking for confusion between a public interface and your private implementation details. Other problems that can arise are users not realizing that the value might change out from under them, subclasses redefining the public getter without realizing that the private setter is not available, and so on.
If you don't try to share the name, then it's clearer that there are two things to think about: your underlying data, and the public interface that relies upon that data.
That's not so bad, is it?
Alternatively, you can use various tricks to make things look somewhat nicer if you really insist on using this pattern. For example, if you have a bunch of fields you could
which will let anything in class C set
data.value
andcomment.value
, and only let everyone else read them. (Edit: modified to include all the goodies in the original example, since if you omit them you might make errors.)