假设我有这个自限定的Java类:
public class SelfBounded<SELF extends SelfBounded<SELF>> {
public String testField = "TEST";
public SelfBounded() {
}
public SELF testMethod() {
return (SELF) this;
}
}
在Java中,我们可以这样写:
public class SelfBoundedMainJava {
public static void main(String[] args) {
SelfBounded selfBounded = new SelfBounded();
System.out.println(selfBounded.testField);
System.out.println(selfBounded.testMethod().testField);
}
}
这段代码按照我的期望输出了2个"TEST"。
但是我在尝试用scala编写同样的代码时遇到了困难:
如果我不传递类型,它会成为SelfBounded [Nothing]。但是,如果我试图使用SelfBounded类型,例如': SelfBounded [SelfBounded]',由于递归性质,编译器会告诉我每个'SelfBounded'都需要一个类型参数,这让我感到很困扰。
object SelfBoundedMainScala extends App {
// here the type of selfBounded1 is SelfBounded[Nothing]
val selfBounded1 = new SelfBounded()
println(selfBounded1.testField)
// does not work since testMethod() returns a Nothing
//println(selfBounded1.testMethod().testField)
// trying to set the type of the selfBounded variable gives me issues because of the recursive type
// does not compile 'class SelfBounded takes type parameters'
// val selfBounded2: SelfBounded[SelfBounded] = new SelfBounded()
// same issue
// val selfBounded3 = new SelfBounded[SelfBounded]()
}
有没有办法在不改变SelfBounded类定义的情况下,在Scala中完成Java主类?
注意:这是一个简化的例子,尝试在Scala中使用testcontainers-java。
this.type
实现相同的结果,但Java没有相应的功能。 - Alexey Romanov