2017-05-08 4 views
0

私はスカラプログラムであるakka-httpを実行しようとしています。次のように私のKISSコードは次のとおりです。 -違法継承、スーパークラスXはmixinの特性のスーパークラス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の素晴らしい設計から継承されているため、直接的または間接的に複数のクラスを拡張することはできません。 できるだけ多くの特性を混在させることは可能ですが、階層内にスーパークラスを1つしか持てません(技術的には、複数のクラスを持つことができますが、それらはすべて互いに拡張する必要があります) SprayBootがサブクラスではないと不平を言っている)。

あなたの場合、クラスであるSprayCanBoot02RestInterface02)も拡張しています(AncileDemoGateway02)。これもクラスです。だからMyBoot02は一度に2つの異なるクラスを拡張しようとしていますが、これは違法です。

AncileDemoGateway02にするとエラーを修正する必要があります。

2

おかげで、クラスから継承する形質は、一般的形質が中に混合することができるクラスを制限するために使用されます。あなたのケースでは

MyBoot02RestInterface02は同じスーパークラスを共有していないので、MyBoot02クラスは、RestInterface02形質を拡張することはできません。

+0

あなたはあなたの答えへの参照を提供できますか?私はそれがここの問題だとは思わない。ありがとう – BigDataScholar

+0

'MyBoot02'のスーパークラスは' SprayCanBoot02'で、 'RestInterface02'のスーパークラスは' AncileDemoGateway02'です。 'SprayCanBoot02'は' AncileDemoGateway02'のサブクラスではないので、コンパイルされません。 – Sebastian

関連する問題