Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么来自Java can';我不在scala工作?_Java_Scala_Types - Fatal编程技术网

为什么来自Java can';我不在scala工作?

为什么来自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

给定以下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: 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();