scala - 非法继承,父类(super class) X 不是 mixin 特征 Z 的父类(super class) Y 的子类 - Scala

标签 scala inheritance akka-http

我正在尝试执行一个 akka-http,它是一个 scala 程序。我的 KISS 代码如下:-

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 不是父类(super class) AncileDemoGateway 的子类”。在我的代码中,SprayCanBoot 和 AncileDemoGateway 是 2 个独立的实体,那么为什么会出现这样的错误?

谢谢

最佳答案

不幸的是,出于某种神秘的原因,这可能是继承自 Java 的出色设计,您不能直接或间接扩展多个类。 可以根据需要混合任意数量的特征,但是在层次结构中只能有一个父类(super class)(好吧,从技术上讲,您可以拥有多个,但它们都必须相互扩展 - 这就是您的错误消息的原因提示 SprayBoot 不是子类)。

在您的情况下,您正在扩展 SprayCanBoot02,这是一个类,以及 RestInterface02,它扩展了 AncileDemoGateway02,这也是一个类(class)。所以 MyBoot02 试图同时扩展两个不同的类,这是非法的。

AncileDemoGateway02 设为特征应该可以修复错误。

关于scala - 非法继承,父类(super class) X 不是 mixin 特征 Z 的父类(super class) Y 的子类 - Scala,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43857743/

相关文章:

java - 这真的是从 Java 将 void 函数传递给 Scala 方法的方法吗?

scala - 有没有办法重写 Spark RDD distinct 以使用 mapPartitions 而不是 distinct?

javascript - ES6 JS 类中的属性继承

javascript - 实例化对象中不存在作为属性的子类方法

scala - Gradle 错误 : Project with path '' could not be found in root project ''

performance - 使用加特林将条件放入 HTTP 请求中

mysql - 如何在 Scala 模板中处理 &

c++ - C++ 子类不能从父类继承私有(private)成员?

docker - 无法从外部访问 Docker 容器中的应用程序

scala - akka-http 和 JsonEntityStreamingSupport