守则:
class Rectangle(l:Double, w:Double)
{
require (l>0, w>0)
val length = l
val width = w
def this (l:Double) = this (l, l)
def area = (length * width)
}
Scala口译员:
class Rectangle(l:Double, w:Double)
[parsing <console>]
[superaccessors in 7ms]
[selectiveanf in 0ms]
[erasure in 4ms]
[Generate ICode from the AST in 5ms]
[inliner in 0ms]
defined class Rectangle
val R = new Rectangle (12, 12)
[parsing <console>]
[erasure in 6ms]
[lazyvals in 0ms]
[Generating icode for <console>]
[Generate ICode from the AST in 4ms]
[inliner in 0ms]
R: Rectangle = Rectangle@117cc9d
val l = println("Rectangle length is " + R.length)
[parsing <console>]
[loaded class file /home/optimight/.eclipse/org.eclipse.platform_3.7.0_155965261/ configuration/org.eclipse.osgi/bundles/201/1/.cp/lib/scala-library.jar(scala/collection/TraversableOnce.class) in 0ms]
[loaded class file /home/optimight/.eclipse/org.eclipse.platform_3.7.0_155965261/configuration/org.eclipse.osgi/bundles/201/1/.cp/lib/scala-library.jar(scala/collection/immutable/IndexedSeq.class) in 0ms]
[total in 44ms]
val A = R.area
[parsing <console>]
<console>:9: error: value area is not a member of Rectangle
该图像显示了Eclipse Scala IDE中的Rectangle和scala解释器的完整代码。
请指导。
答案 0 :(得分:2)
在hires(here)中快速查看屏幕截图后,您似乎并没有使用您在解释器中定义的类。
解释器启动时会加载Rectangle
类,并在键入时重新定义它:class Rectangle(l: double, w: double)
。要么未加载该类,并且您是第一次定义它。
在第一种情况下,你只需要避免在解释器中键入第一行,在第二种情况下,你必须将类加载到解释器中(我不熟悉scala IDE解释器,所以我可以'告诉你该怎么做。)