2016-02-28 74 views
0

我是一个没有java背景的scala初学者。我不了解导入系统。 我有我的应用程序,在这里我使用进口导入类:错误:未找到:对象控制器

import Array._ 
import List._ 
import Controller.api 
object scalaStart{ 
    def main(args: Array[String]){ 
      var apiCtrl = new api() 
      apiCtrl.getById(1) 
      println(apiCtrl.title) 
    } 

} 

这是类:

package Controller 
class api { 
    var id:Int 
    var title:String 
    var description:String 
    def getById(id:Int){ 
    if(id = 1){ 
     this.id   = 1 
     this.title  = s"Title Nummer ${this.id}" 
     this.description = s"Description Nummer ${this.id}" 
    }else{ 
     this.id   = 1 
     this.title  = s"Artikel mit der ID: ${this.id} existiert nicht." 
     this.description = s"Kein Eintrag mit der ID: ${this.id}" 
    } 
    } 
} 

我还检查仅进口API和进口控制器和通配符controller._ controller.api._ 。

+0

什么是您的目录布局?你在用sbt吗? – Daenyth

+0

不,我不使用sbt。 GettingStart/src/controller/api.scala和GettingStart/src/GettingStarted/GettingStarted.scala。它是用Netbeans和scala Plugin自动构建的。 –

+0

对于区分大小写的文件系统,大小写在包名中可能很重要。成语是软件包是小写的,类是从Upper开始的。 –

回答

1

Philipp,你的代码实际上不能编译。 在行if(id=1){它应该是if(id==1)。 尝试纠正这一点并重建您的项目。 你会发现,编译器会给你另一个错误:

Error:(5, 7) class api needs to be abstract, since: it has 3 unimplemented members. /** As seen from class api, the missing signatures are as follows. * For convenience, these are usable as stub implementations. */ def description_=(x$1: String): Unit = ??? def id_=(x$1: Int): Unit = ??? def title_=(x$1: String): Unit = ??? class api { ^

这是因为在Scala中你不能离开变量声明为抽象的,你可以用Java做的。而不是var id:Int你需要做些什么var id:Int = 0和其他声明的变量相同。

+0

谢谢。大愚蠢的失败。我初始化了字符串:var id:Int = 0 var title:String =“” var description:String =“”'然后我编译了控制器包,它工作正常。但是我不能像这样使用字符串插值:'this.title = s“Title Nummer {this.id}”' –

+0

不客气。 对我来说,在我解决了所有这些问题之后,当我跑步时,它给了我: 'Title Nummer 1' 对你而言,它是一样的吗? –

+0

我收到错误:';'预期但发现字符串文字。 this.title = s“Title Nummer $ {this.id}”我有斯卡拉版本2.9 –