2017-05-08 34 views
0

我试图执行一个akka-http,它是一个scala程序。我的吻代码如下: -非法继承,超类X不是mixin特征Z的超类Y的子类Z-Scala

import akka.actor.ActorSystem 
import akka.http.scaladsl.Http 
import akka.http.scaladsl.model.{HttpRequest, HttpResponse} 
import akka.http.scaladsl.server.Directives._ 
import akka.http.scaladsl.server.directives.BasicDirectives 
import akka.stream.ActorMaterializer 
import akka.stream.scaladsl.Flow 
import com.typesafe.config.ConfigFactory 


object MyBoot02 extends SprayCanBoot02 with RestInterface02 with App { 

} 

abstract class SprayCanBoot02 { 


    val config = ConfigFactory.load() 
    val host = config.getString("http.host") 
    val port = config.getInt("http.port") 


    implicit val system = ActorSystem("My-ActorSystem") 
    implicit val executionContext = system.dispatcher 
    implicit val materializer = ActorMaterializer() 
    //implicit val timeout = Timeout(10 seconds) 

    implicit val routes: Flow[HttpRequest, HttpResponse, Any] 

    Http().bindAndHandle(routes, host, port) map { 
    binding => println(s"The binding local address is ${binding.localAddress}") 
    } 
} 

trait RestInterface02 extends AncileDemoGateway02 with Resource02 { 

    implicit val routes = questionroutes 
    val buildMetadataConfig = "this is a build metadata route" 
} 

trait Resource02 extends QuestionResource02 

trait QuestionResource02 { 
    val questionroutes = { 
    path("hi") { 
     get { 
     complete("questionairre created") 
     } 
    } 
    } 
} 

class AncileDemoGateway02 { 
    println("Whatever") 
} 

,我得到试图执行MyBoot02的时候是因为如何,我的东西接线错误。错误如下:

Error:(58, 41) illegal inheritance; superclass SprayCanBoot is not a subclass of the superclass AncileDemoGateway of the mixin trait RestInterface object MyBoot extends SprayCanBoot with RestInterface with App

为什么错误状态“SprayCanBoot不超AncileDemoGateway的子类”。在我的代码中,SprayCanBoot和AncileDemoGateway是2个独立的实体,那么为什么会出现这样的错误?

由于

回答

2

不幸的是,由于一些神秘的原因,这可能是从Java的精彩设计继承而来的,您不能直接或间接地扩展多个类。 可以根据需要混合尽可能多的特征,但在层次结构中只能有一个超类(在技术上,可以有多个超类,但它们都必须相互扩展 - 这就是为什么您的错误消息抱怨SprayBoot不是子类)。

在你的情况下,你扩展SprayCanBoot02,这是一个类,也RestInterface02,延伸AncileDemoGateway02,这也是一个类。所以MyBoot02正试图一次扩展两个不同的类,这是非法的。

制作AncileDemoGateway02一个特质应该修复错误。

2

通常,性状从一个类继承通常用于限制类的性状可以混入。

对于您的情况,MyBoot02类无法扩展RestInterface02特征,因为MyBoot02RestInterface02不共享相同的超类。

+0

你能否提供一个参考答案。我认为这不是问题。谢谢 – BigDataScholar

+0

MyBoot02的超类是SprayCanBoot02,RestInterface02的超类是AncileDemoGateway02。由于'SprayCanBoot02'不是'AncileDemoGateway02'的子类,因此它不能编译。 – Sebastian