简体   繁体   English

Scala构造函数中有与Java等效的吗?

[英]Is there an equivalent of Java's this in Scala constructors?

I have a Scala 2.11 constructor where I want one of the class' attributes to have the same name as one of the parameters in the constructor. 我有一个Scala 2.11构造函数,我希望该类的属性之一与该构造函数中的参数之一具有相同的名称。

class Transition( next: Int ) {
    this.next = next // works in Java
}

This doesn't compile. 这不会编译。 I'm wondering what the equivalent of the assignment statement is in Scala. 我想知道Scala中的赋值语句等同于什么。

Scala makes your life easy: Scala使您的生活变得轻松:

class Transition(val next: Int)

The val will make the constructor parameter into a field. val将使构造函数参数成为字段。

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

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