我對正確理解類和伴侶對象的使用存在問題。案例分類和伴隨對象
當定義一個case類時,它的陪伴對象隨它而來,但是定義一個與case類同名的對象的結果是什麼?它是否覆蓋伴侶對象?以及如何訪問案例類參數?
例如在TestCaseClass.scala文件I定義如下:
case class TestCaseClass(att1: String, att2: Int, att4s: List[String])
object TestCaseClass {
def iWantDoSomethingWithMyParams: String = {
att1 + " " + att2
}
// Other functions
}
object AnotherTestCaseClass {
def iWantDoSomethingWithTestCaseClassParams: String = {
// How to access TestCaseClass.att1
TestCaseClass.att1 + " " + TestCaseClass.att2
}
def iWantGetAllAttr4: List[String] = {
// ???
}
}
http://stackoverflow.com/questions/5827510/how-to-override-apply-in-a-case-class-companion –