斯卡拉继承和对象创建

问题描述:

我试图做到这一点在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? 
+0

你写了“抽象课室”。它应该是“阶级”,而不是“阶级”。在评论中还是在你的程序中是错字? – vbezhenar 2012-07-08 06:50:22

+0

twas只是一个错字 – adrianton3 2012-07-08 06:56:08

你的代码没有错。下面是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([email protected], null) 

scala> 

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

对于具有人同样的问题: Room.scala可能会驻留在包房中,但不要忘记在Room.scala的标题中声明。在Java中,你从来没有遇到过这个错误,因为Java迫使你保持严格的目录结构