繁体   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