Scala 构造函数

发布于 2024-10-17 06:02:11 字数 259 浏览 2 评论 0原文

Scala 中的以下 Java 代码相当于什么:

import java.util.Random;

public class Bool {

 private boolean door;
 Random random = new Random();

 Bool() {
  this.door = random.nextBoolean();
 }
}

因此,当创建一个新的 Bool 对象时,door 变量将自动获取一个随机布尔值。

What is the equivalent of the below Java code in Scala:

import java.util.Random;

public class Bool {

 private boolean door;
 Random random = new Random();

 Bool() {
  this.door = random.nextBoolean();
 }
}

So when a new Bool object is created, the door variable will automatically get a random Boolean value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野却迷人 2024-10-24 06:02:11

在 Scala 中,类的主体相当于 Java 中构造函数调用的方法。因此,您的类将如下所示:(

import java.util.Random

class Bool {
    private val random = new Random
    private val door = random.nextBoolean()

    ... // method definitions, etc.
}

请注意,要挑剔,因为您没有声明 Java 变量 final,有人可能会认为字段应该是 varrandom 字段是受包保护的,这看起来像是一个疏忽,并且会在 Scala 中呈现为 protected[pkgName] 其中 pkgName 是类包中最具体组件的名称。)

In Scala, the body of the class is equivalent to the methods invoked by a constructor in Java. Hence your class would look something like the following:

import java.util.Random

class Bool {
    private val random = new Random
    private val door = random.nextBoolean()

    ... // method definitions, etc.
}

(note that to be picky, since you didn't declare your Java variables final, one could argue that the fields should be vars here instead. Additionally, your random field is package-protected which looks like an oversight, and would be rendered in Scala as protected[pkgName] where pkgName is the name of the most specific component of the class' package.)

她说她爱他 2024-10-24 06:02:11

这是我的看法:

case class MyBool(door: Boolean = Random.nextBoolean)

这使得创建具有特定门值的 MyBool 新实例成为可能,例如:

val x1 = MyBool() // random door
val x2 = MyBool(true) // door set explicitly

由于只能有两个不同的门值,因此使用静态对象是有意义的相反,像这样:

sealed trait MyBool {
  def door:Boolean
}
object MyBool {
  case object True extends MyBool {
    def door = true
  }

  case object False extends MyBool {
    def door = false
  }

  def apply:MyBool = if(Random.nextBoolean) True else False
}

用法:

val x1 = MyBool() // random door value
val x2 = MyBool.True // explicit door value

Here is my take:

case class MyBool(door: Boolean = Random.nextBoolean)

This leaves open the possibility to create a new instance of MyBool with a certain door value, e.g.:

val x1 = MyBool() // random door
val x2 = MyBool(true) // door set explicitly

Since there can only be two different door values, it would make sense to use static objects instead, like this:

sealed trait MyBool {
  def door:Boolean
}
object MyBool {
  case object True extends MyBool {
    def door = true
  }

  case object False extends MyBool {
    def door = false
  }

  def apply:MyBool = if(Random.nextBoolean) True else False
}

Usage:

val x1 = MyBool() // random door value
val x2 = MyBool.True // explicit door value
好听的两个字的网名 2024-10-24 06:02:11

更接近的 scala 代码应该是:

class Bool {
  var random = new Random
  private var door = random.nextBoolean
}

即使公共随机字段看起来不是一个好主意。

The closer scala code should be:

class Bool {
  var random = new Random
  private var door = random.nextBoolean
}

Even if the public random field does not look as a good idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文