【发布时间】:2017-06-07 12:13:50
【问题描述】:
我正在学习 Scala,我试图从不同类的 2 个脚本导入,但是,我得到一个错误,我不知道为什么。
Summer.scala 中的这个类是我尝试从 ChecksumAccumulator 类导入单例对象的地方。
import ChecksumAccumulator.calculate
object Summer{
def main(args: Array[String]): Unit= {
for (arg <- args)
println(arg + " : " + calculate(arg))
}
}
类 ChecksumAccumulator,带有单例对象 ChecksumAccumulator。
object ChecksumAccumulator {
/*
When a singleton object shares the same name
with a class, it is called that class's companion object.
*/
private val cache = mutable.Map.empty[String, Int]
def calculate(s: String): Int =
if (cache.contains(s))
cache(s)
else {
val acc = new ChecksumAccumulator
//The object with the same name of the class can acces to the private members.
//println(acc.sum)
for (c <- s)
acc.add(c.toByte)
val cs = acc.checksum()
cache += (s -> cs)
cs
}
def showMap() : Unit = {
for(base:String <- cache.keys)
println(cache(base))
}
//def showMap(): String = { var cadena = cache("Every value is an object.") + " "+ cache("Every value is an object. per second time!!"); cadena }
}
这个脚本抛出这个错误
scala Summer.scala
Summer.scala:8: error: not found: object ChecksumAccumulator import ChecksumAccumulator.calculate ^ Summer.scala:14: 错误:未找到: 值计算 println(arg + " : " + calculate(arg)) ^ 两个错误 找到了
【问题讨论】:
标签: scala