Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a custom logic to a Scala's constructor

Tags:

scala

Suppose I have a class

  class MyClass(a: Int, b: String) {
   //....
  }

If I want to add some custom logic (code) to this constructor, how do I do that?

like image 934
Alan Coromano Avatar asked Dec 02 '22 23:12

Alan Coromano


1 Answers

class MyClass(a: Int, b: String) {
   // this is the constructor right here
   println("Hi, i'm the constructor")

   def imAMethod = 1

   println("Hi, I'm also part of the constructor down here, the whole class body is")
}
like image 132
stew Avatar answered Jan 10 '23 10:01

stew