Extending class with parameter
I noticed tha开发者_运维知识库t I can extend a class and at the same time pass parameters to the extended class:
scala> class SomeClass(val str: String)
defined class SomeClass
scala> class SubclassOne extends SomeClass("one")
defined class SubclassOne
scala> class SubclassTwo extends SomeClass("two")
defined class SubclassTwo
scala> val obj : SomeClass = new SubclassOne
obj: SomeClass = SubclassOne@2dca4eb4
scala> obj.str
res0: String = one
I'm working a lot with case classes, where ClassName(args)
actually creates an object, so to me it looks like I'm extending an object, but I'm not sure here.
Does it perhaps mean that I'm extending the class, and automatically pass an argument to the super constructor?
Does it perhaps mean that I'm extending the class, and automatically pass an argument to the super constructor?
Yes, that is exactly what it means.
精彩评论