2017-05-08 40 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