2016-11-26 24 views
0

我有2个问题:无法获取斯卡拉文件运行

通过调用scalac然后斯卡拉或打印使用REPL但我

1)我试图获取列表菜单这个计划出现任何有点困惑,因为它使用包。我试着用

scalac Fruits.scala 
scala bobsdelight\Fruits 

运行,但我得到java.lang.NoClassDefFoundError: bobsdelights\Fruits wrong name: bobsdelights/Fruits)

如果有人能请告诉我如何执行这个脚本,将是巨大

2)我还试图建立一个由第一加载文件要求在REPL new Fruits.Apple苹果新对象,但我得到:

error: type Apple is not a member of object Fruits 
new Fruits.Apple`` 

这个例子是在编程在斯卡拉本书。

package bobsdelights 

abstract class Fruit(
    val name: String, 
    val color: String 
) 

object Fruits { 
    object Apple extends Fruit("apple", "red") 
    object Orange extends Fruit("orange", "orange") 
    object Pear extends Fruit("pear", "yellowish") 
    val menu = List(Apple, Orange, Pear) 
} 
+0

您可以删除Fruits.scala的完整内容吗? – Pavel

+0

就是这一切。 – zam

+0

创建实际的类定义水果。 Singelton有不同的课程,比如Fruits $等。 – Pavel

回答

1

REPL例如:

$ scala 
Welcome to Scala 2.12.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_111). 
Type in expressions for evaluation. Or try :help. 

scala> :pa bobsdelight.scala 
Pasting file bobsdelight.scala... 

scala> Fruits.menu 
<console>:12: error: not found: value Fruits 
     Fruits.menu 
    ^

scala> import bobsdelight._ 
<console>:11: error: not found: value bobsdelight 
     import bobsdelight._ 
      ^

scala> import bobsdelights._ 
import bobsdelights._ 

scala> Fruits.menu 
res1: List[bobsdelights.Fruit] = List([email protected], [email protected], [email protected]) 

如果您尝试没有主方法 “运行” 一类:

$ scala bobsdelights.Fruits 
java.lang.NoSuchMethodException: bobsdelights.Fruits.main([Ljava.lang.String;) 

成语可运行的应用程序:

object Fruits extends App { 
    object Apple extends Fruit("apple", "red") 
    object Orange extends Fruit("orange", "orange") 
    object Pear extends Fruit("pear", "yellowish") 
    val menu = List(Apple, Orange, Pear) 

    println(menu) 
} 

$ scalac bobsdelight.scala && scala bobsdelights.Fruits 
List([email protected], [email protected], [email protected])