简体   繁体   中英

Scala inheritance and object creation

I'm trying to do this in Scala, but for some reason it won't work

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)"
 ...
}

and also...

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

There's nothing wrong in your code. Here's an output of REPL which proves that:

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>

The problem must be in how you placed it in a package, which one, in which file under which directory. You should broaden your question with this info.

For anyone having the same issue: Room.scala may reside in package Room, but don't forget to declare that in the header of Room.scala too. In Java you never come across this error as Java forces you to keep a strict directory structure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM