码迷,mamicode.com
首页 > 其他好文 > 详细

Scala中关于auxiliary constructors

时间:2019-07-28 13:40:08      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:net   ons   a star   compare   ever   poi   event   either   first   

  In Scala, constructors other than the primary constructor are called auxiliary constructors.

  Auxiliary constructors in Scala start with def this(...).

 

  In Scala, every auxiliary constructor must invoke another constructor of the same class as its first action. In other words, the first statement in every auxiliary

constructor in every Scala class will have the form "this(...)". The invoked constructor is either the primary constructor (as in the Rational example), or another

auxiliary constructor that comes textually before the calling constructor. The net effect of this rule is that every constructor invocation in Scala will end up

eventually calling the primary constructor of the class. The primary constructor is thus the single point of entry of a class.

  Note

  If you‘re familiar with Java, you may wonder why Scala‘s rules for constructors are a bit more restrictive than Java‘s. In Java, a constructor must either invoke

another constructor of the same class, or directly invoke a constructor of the superclass, as its first action. In a Scala class, only the primary constructor can

invoke a superclass constructor. The increased restriction in Scala is really a design trade-off that needed to be paid in exchange for the greater conciseness and

simplicity of Scala‘s constructors compared to Java‘s.

 

Scala中关于auxiliary constructors

标签:net   ons   a star   compare   ever   poi   event   either   first   

原文地址:https://www.cnblogs.com/25th-engineer/p/11258413.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!