簡體   English   中英

Scala繼承和對象創建

[英]Scala inheritance and object creation

我正在嘗試在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?

您的代碼沒有錯。 這是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>

問題必須出在如何將其放在一個包中,哪個文件,哪個文件的目錄下。 您應該使用此信息擴大您的問題。

對於存在相同問題的任何人:Room.scala可能駐留在Room包中,但也不要忘記在Room.scala的標頭中聲明它。 在Java中,您永遠不會遇到此錯誤,因為Java會強制您保持嚴格的目錄結構

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM