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
Friday, May 25, 2012
Overloading primary constructor in Scala
Labels:
Scala
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment