-1

我正在尝试在 Scala 中执行此操作,但由于某种原因它不起作用

abstract class Room {
 ...
}

class RoomA1 extends Room { //"not found: type Room" 
//but they're in the same package!!!
//and if I import it as Eclipse suggests the import declaration will give 
//"Room is not a member of rooms(rooms.type)"
 ...
}

并且...

var room = new Array[Room](2)
room(0) = new RoomA1 //gives a type mismatch 
//how can I accomplish this?
4

2 回答 2

3

您的代码没有任何问题。这是 REPL 的输出,它证明:

scala> abstract class Room
defined class Room

scala> class RoomA1 extends Room
defined class RoomA1

scala> val room = new Array[Room](2)
room: Array[Room] = Array(null, null)

scala> room(0) = new RoomA1

scala> room
res3: Array[Room] = Array(RoomA1@71c0ef03, null)

scala>

问题一定在于你如何将它放在一个包中,哪个包,哪个目录下的哪个文件。您应该使用此信息扩大您的问题。

于 2012-07-08T07:34:05.550 回答
0

对于任何有同样问题的人:Room.scala 可能驻留在包 Room 中,但不要忘记在 Room.scala 的标头中声明它。在 Java 中,您永远不会遇到此错误,因为 Java 会强制您保持严格的目录结构

于 2012-07-08T07:27:51.683 回答