4

假设我有一堂课

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

如果我想向这个构造函数添加一些自定义逻辑(代码),我该怎么做?

4

1 回答 1

17
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")
}
于 2013-06-04T03:45:20.130 回答