我试图在类中实现(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'.
任何人都可以指出我为什么会失败,如何解决它的最佳方法?
从 IntelliJ 运行播放应用程序时,选中“使用 sbt shell”时,VM 参数和环境变量在运行配置中显示为灰色。如何将 env 变量传递给 sbt 任务,然后取消选中“作为 sbt 任务运行”复选框或在机器上全局设置 env 变量?
我有一个案例类,其中一些逻辑约束require
在案例类主体中作为 s 实现。当尝试从表示语法正确但逻辑上无效的实例的 JSON 中解码此案例类时,异常实际上是在调用线程上引发的,而不是作为Left
from的返回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。