I want to write class whose constructor takes two parameters, but the arguments are not actually members of the class. e.g.
class P(V1:Int, V2:Int) {
val set = Set(V1, V2)
}
Having constructed the 'set', I don't actually care about V1 开发者_开发知识库and V2. Is there a way of expressing this in Scala ?
Well, exactly like that. If the constructor arguments are not tagged with val
or var
, nor the class is a case class
, then they'll be kept allocated if used inside methods (or lazy val
, I suppose). If used just in the constructor, they won't be allocated with the object, not even as private fields.
精彩评论