小编iDP*_*WF1的帖子

在实现中覆盖接口方法返回类型和派生类

我试图在类中实现(C#)接口方法,返回派生类型而不是接口中定义的基类型:

interface IFactory
{
    BaseCar GetCar();
}

class MyFactory : IFactory
{
    MyCar GetCar()
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

当然在哪里:

class MyCar : BaseCar
{

}
Run Code Online (Sandbox Code Playgroud)

但是,会发生以下错误:

'MyFactory'没有实现接口成员'IFactory.GetCar()'.'MyFactory.BaseCar()'无法实现IFactory.GetCar()',因为它没有匹配的返回类型'BaseCar'.

任何人都可以指出我为什么会失败,如何解决它的最佳方法?

c# polymorphism interface

19
推荐指数
3
解决办法
1万
查看次数

无法将环境变量传递给 intellij 中的 sbt 任务

从 IntelliJ 运行播放应用程序时,选中“使用 sbt shell”时,VM 参数和环境变量在运行配置中显示为灰色。如何将 env 变量传递给 sbt 任务,然后取消选中“作为 sbt 任务运行”复选框或在机器上全局设置 env 变量?

scala intellij-idea sbt playframework

5
推荐指数
1
解决办法
2837
查看次数

使用 circe 在类主体中使用“require”解码案例类会引发异常,而不是返回“Left”

我有一个案例类,其中一些逻辑约束require在案例类主体中作为 s 实现。当尝试从表示语法正确但逻辑上无效的实例的 JSON 中解码此案例类时,异常实际上是在调用线程上引发的,而不是作为Leftfrom的返回decode

重现代码片段:

case class Foo(bar: String) {
  require(bar.length < 5)
}

object Sandbox extends App {
  import io.circe.generic.auto._
  import io.circe.parser._

  val foo1 = decode[Foo](""" {"bar":"abc"} """)
  println(s"foo1: $foo1")

  //expected: Left(IllegalArgumentException("requirement failed"))
  //actual: throws IllegalArgumentException("requirement failed")
  val foo2 = decode[Foo](""" {"bar":"abcdefg"} """) 
  println(s"foo2: $foo2")
}
Run Code Online (Sandbox Code Playgroud)

是否可以让此异常返回而不Left抛出decode?欢迎任何想法/建议...

TIA

M。

json scala circe

1
推荐指数
1
解决办法
456
查看次数

标签 统计

scala ×2

c# ×1

circe ×1

intellij-idea ×1

interface ×1

json ×1

playframework ×1

polymorphism ×1

sbt ×1