我有两个问题:
1)我试图让这个程序中的List 菜单通过调用scalac然后scala或使用REPL打印出现但是我有点困惑,因为它使用包。我尝试使用
运行它scalac Fruits.scala
scala bobsdelight\Fruits
但我得到java.lang.NoClassDefFoundError: bobsdelights\Fruits wrong name: bobsdelights/Fruits)
如果有人可以请告诉我如何执行这个非常棒的脚本
2)我还试图通过首先加载文件来调用REPL中的new Fruits.Apple
来创建一个新的Apple对象,但我得到了:
error: type Apple is not a member of object Fruits
new Fruits.Apple``
此示例位于Scala编程书中。
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 :(得分: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(bobsdelights.Fruits$Apple$@6c17c0f8, bobsdelights.Fruits$Orange$@260e3837, bobsdelights.Fruits$Pear$@88b76f2)
如果你试图&#34;运行&#34;没有主要方法的课程:
$ 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(bobsdelights.Fruits$Apple$@4f8e5cde, bobsdelights.Fruits$Orange$@504bae78, bobsdelights.Fruits$Pear$@3b764bce)