如何在 Scala 中使用属性?
是的,您可以通过setName设置属性名称并通过getName获取属性名称。 但是 C# 中这样的属性怎么样:
int Name{
get{return name;}
set{name = value;}
}
或
Name{get; set;}
(auto property)
我想知道 Scala 中是否存在这样的东西。搜了一下没有任何信号。
Yes, you can set a property name by setName and get it by getName.
But what about property like this in C#:
int Name{
get{return name;}
set{name = value;}
}
or
Name{get; set;}
(auto property)
I wonder if such thing exists in Scala. Googling around without any signals.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了完整起见,如果您只想要不可变字段(推荐!),
相同
则与不可变字段/属性 。 “case”关键字自动使构造函数参数成为 val,并添加其他好处。
Just for completeness, if you only want immutable fields (recommended!)
is the same thing as
with regards to immutable fields/properties. The "case" keyword automatically makes the constructor arguments vals, as well as adding other goodies.
如果你想要 java get/set 方法,你可以使用 scala.reflect.BeanProperty
You can use scala.reflect.BeanProperty if you want java get/set methods