Don't forget to also checkout my second blog containing articles to all other related ICT topics!!

Friday, May 25, 2012

Overloading primary constructor in Scala

scala> class Employee(employeeId: Int, isFulltimeEmployed: Boolean) {
     |     /** we can overload the primary constructor **/
     |     def this(employeeId: Int) = this(employeeId, true)
     |     override def toString() = employeeId + " works " + (if (isFulltimeEmployed) "fulltime" else "parttime")
     | }
defined class Employee

scala>

scala> val john = new Employee(12545, true)
john: Employee = 12545 works fulltime

scala> val belinda = new Employee(32567, false)
belinda: Employee = 32567 works parttime

scala> val herman = new Employee(56784)
herman: Employee = 56784 works fulltime

No comments:

Post a Comment