在Scala程序中定义诸如Pi或黄金比率之类的常量是正确的吗?
举个例子,在C#中我可以这样做:
class Example
{
public readonly static Double GoldenRatio;
static Example ()
{
GoldenRatio = (1.0 + Math.Sqrt (5.0)) / 2.0;
}
}
Run Code Online (Sandbox Code Playgroud)
Ion*_*tan 13
它只是一个val成员:
object Example {
val GoldenRatio = (1.0 + Math.sqrt(5.0)) / 2.0
}
Run Code Online (Sandbox Code Playgroud)
另外,请查看有关常量的Scala样式指南部分.
| 归档时间: |
|
| 查看次数: |
9035 次 |
| 最近记录: |