这是将Java接口转换为Scala的正确方法吗? [英] Is this the correct way of translating Java interface into Scala?

查看:190
本文介绍了这是将Java接口转换为Scala的正确方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习Scala,我将做一个简单的交叉编译器。

I am starting to learn Scala and I will do a simple cross compiler.

我将支持一小组指令,如print。

I will support a small set of instructions like print.

注意:代码片段未经过测试或编译。

以下是我在JAVA中所做的事情。

Note: the code snippets are not tested or compiled.
Here is what I would do in 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方式

Is the snippet below the correct "Scala way"?

trait Compiler {
  var printInstruction: String
}
class JavaCompiler extends Compiler {
  var printInstruction = "System.out.print(arg0);"
}
class ScalaCompiler extends Compiler {
  var printInstruction = "print(arg0);"
}

编辑:

我将把我的第二个问题转移到一个新线程。

I will move my second question to a new thread.

推荐答案

用于1:1映射,那些 var s应更改为 def s。

For a 1:1 mapping, those vars should be changed to defs.

trait Compiler {
  def printInstruction: String
}

class JavaCompiler extends Compiler {
  def printInstruction = "System.out.print(arg0);"
}

class ScalaCompiler extends Compiler {
  def printInstruction = "print(arg0);"
}

def 声明一个方法。如果您不提供实现,它将成为一种抽象方法。

def declares a method. When you don't provide an implementation, it becomes an abstract method.

编辑:

这里使用的技术是一种有效且有用的技术。或者,您可以使用以下两种技术之一来模拟您的问题。

The technique used here is a valid and useful technique. Alternatively you could use one of the following two techniques to model your problem.

1)受歧视的联盟。 (又名总和类型。)

1) Discriminated unions. (aka sum types.)

参考这篇优秀文章了解这个概念。这就是你的例子在以这种方式建模时的样子:

Refer to this excellent article to learn about this concept. This is how your example would probably look like when modeled this way:

sealed trait Compiler {
  def printInstruction: String = this match {
    case JavaCompiler => "System.out.print(arg0);"
    case ScalaCompiler => "print(arg0);"
  }
}

case object JavaCompiler extends Compiler
case object ScalaCompiler extends Compiler

2)输入类型。

这里是Daniel Sobral关于此主题的精彩文章。您可以通过Google搜索术语类型类,模式,Scala,implicits等来挖掘更多内容。如果问题是使用类型类模式建模的话,这就是您的代码的样子:

Here is a great post by Daniel Sobral on this topic. You can dig up a few more by googling the terms type-class, pattern, Scala, implicits etc. This is how your code might look like if the problem's modeled with type class pattern:

trait Compiler[C] {
  def printInstruction(c: C): String
}

case object JavaCompiler

implicit object JavaCompilerIsCompiler extends Compiler[JavaCompiler.type] {
  def printInstruction(c: JavaCompiler.type): String = "System.out.print(arg0);"
}

case object ScalaCompiler

implicit object ScalaCompilerIsCompiler extends Compiler[ScalaCompiler.type] {
  def printInstruction(c: ScalaCompiler.type) = "print(arg0);"
}

对于您的问题,原始方法和受歧视的联盟方法似乎是最好的建模解决方案。

For your problem, the original approach and the discriminated unions approach seem to be the best modeling solutions.

这篇关于这是将Java接口转换为Scala的正确方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆