简体   繁体   中英

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.

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 makes your life easy:

class Transition(val next: Int)

The val will make the constructor parameter into a field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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