2012-07-08 137 views
-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? 
+0

你寫了「抽象課室」。它應該是「階級」,而不是「階級」。在評論中還是在你的程序中是錯字? – vbezhenar 2012-07-08 06:50:22

+0

twas只是一個錯字 – adrianton3 2012-07-08 06:56:08

回答

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([email protected], null) 

scala> 

的問題必須是你如何把它放在一個包,其中之一,在哪個文件下的目錄。你應該用這個信息擴大你的問題。

0

對於具有人同樣的問題: Room.scala可能會駐留在包房中,但不要忘記在Room.scala的標題中聲明。在Java中,你從來沒有遇到過這個錯誤,因爲Java迫使你保持嚴格的目錄結構