简体   繁体   English

在Scala中使用抽象成员类实现抽象的通用Java类

[英]Implementing an abstract, generic Java class with abstract member class in Scala

I'm trying to implement an abstract, generic Java class in Scala, and I'm getting an error of the form 我正在尝试在Scala中实现一个抽象的通用Java类,并且我收到了表单的错误

object creation impossible, since method B in class A of type (x$1: A<Concrete1, Concrete2>#C)Unit is not defined

The library classes that I'm implementing look like this: 我正在实现的库类看起来像这样:

public abstract class A<T, U> {
    public void B(C);

    public abstract class C {
        // elided
    }
}

and I want to implement an A to hand back to the library (which will call B , supplying a C ). 我想实现一个A来交回库(它将调用B ,提供一个C )。 In Java, I can do: 在Java中,我可以这样做:

new A<Concrete1, Concrete2>() {
    @Override
    public void B(C c) {
        // implementation
    }
}

In Scala, I'm trying 在斯卡拉,我正在努力

new A[Concrete1, Concrete2] {
    def B(c: C): Unit = {
        // implementation
    }
}

and I get the error message at the top. 我在顶部收到错误消息。 Using override , the compiler complains that method B overrides nothing . 使用override ,编译器抱怨method B overrides nothing It seems that the Scala type system isn't recognizing the C I pass in as an A<Concrete1, Concrete2>#C , but I'm not sure what type it does think it is. 看来,Scala的类型系统不能识别C我通过在作为A<Concrete1, Concrete2>#C ,但我不知道是什么类型认为它是。

I've tried specifying self: A[Concrete1, Concrete2] => , as well as a self-type on C : def B(c: self.C , but neither solves the problem. I also tried def B(c: A[Concrete1, Concrete2].C) , but this raises a syntax error. 我试过指定self: A[Concrete1, Concrete2] => ,以及Cdef B(c: self.C上的自我类型def B(c: self.C ,但都没有解决问题。我也试过def B(c: A[Concrete1, Concrete2].C) ,但这会引发语法错误。

Any suggestions? 有什么建议么?

Solved: def B(c: A[Concrete1, Concrete2]#C): Unit . 解决: def B(c: A[Concrete1, Concrete2]#C): Unit Needed the generic type information for this to be a valid override, but I wasn't aware of the # operator. 需要通用类型信息才能成为有效覆盖,但我不知道#运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM