简体   繁体   English

Scala继承和对象创建

[英]Scala inheritance and object creation

I'm trying to do this in Scala, but for some reason it won't work 我正在尝试在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)"
 ...
}

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: 这是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>

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. 对于存在相同问题的任何人:Room.scala可能驻留在Room包中,但也不要忘记在Room.scala的标头中声明它。 In Java you never come across this error as Java forces you to keep a strict directory structure 在Java中,您永远不会遇到此错误,因为Java会强制您保持严格的目录结构

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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