Tag: 特性

特征和接口二进制兼容吗?

我很惊讶Scala在不同版本中是二进制不兼容的事实。 现在,因为在Java 8我们有默认的方法实现,它与我们提供的trait几乎相同,是否可以安全地在Java代码中使用特征? 我自己尝试使用它: trait TestTrait { def method(v : Int) def concrete(v : Int) = println(v) } public class Test implements TestTrait{ // Compile-error. Implement concrete(Int) @Override public void method(int v) { System.out.println(v); } } 但它拒绝编译。 编译器抱怨没有混淆concrete(Int) 。 虽然我在TestTrait指定了实现。

这是将Java接口转换为Scala的正确方法吗?

我开始学习Scala,我将做一个简单的交叉编译器。 我会支持一些像print这样的指令。 注意:代码片段未经过测试或编译。 这是我在JAVA中要做的。 public interface Compiler{ String getPrintInstruction(); } public class JavaCompiler implements Compiler{ public String getPrintInstruction(){ return “System.out.print(arg0);” } } public class ScalaCompiler implements Compiler{ public String getPrintInstruction(){ return “print(arg0);” } } 片段下面是正确的“Scala方式 ”吗? trait Compiler { var printInstruction: String } class JavaCompiler extends Compiler { var printInstruction = “System.out.print(arg0);” } class ScalaCompiler […]