Scala继承和对象创建

时间:2012-07-08 06:39:09

标签: scala inheritance

我正在尝试在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?

2 个答案:

答案 0 :(得分: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>

问题必须在于如何将它放在一个包中,哪个文件位于哪个目录下。你应该用这个信息扩大你的问题。

答案 1 :(得分:0)

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