为什么来自Java can';我不在scala工作?
给定以下Java类定义:为什么来自Java can';我不在scala工作?,java,scala,types,Java,Scala,Types,给定以下Java类定义: class Base { int value; public <T extends Base> T self() { return (T) this; } } class Derived extends Base {} 但此Scala代码无法运行: (new Derived).self(); // runtime error 时间错误: java.lang.ClassCastException: Deriv
class Base {
int value;
public <T extends Base> T self() {
return (T) this;
}
}
class Derived extends Base {}
但此Scala代码无法运行:
(new Derived).self(); // runtime error
时间错误:
java.lang.ClassCastException: Derived cannot be cast to scala.runtime.Nothing$
为什么它不工作,以及如何修复它?在scala中,这种方法看起来像是
self[t我认为您编写了错误的代码。我在JDK1.6中进行了测试
new Derived().self();
(new Derived()).self();
一切都好
但是(新派生).self();
给出编译错误
new Derived().self();
(new Derived()).self();