简体   繁体   中英

Reuse a case class inside an object in scala

I have an object that contains one or more case classes and associated methods. I would like to reuse this case class within another object (which has similar characteristics as this object but also some differentiating methods).

private object abc {
    /* ... */

    case class xyz(..) { def someFunc(){} }

    object xyz { def apply() {} }
}

private object extendabc {

    // How to reuse case class xyz here?
}

If you want to just access you can use this kind of a code .

private object abc {

case class xyz() { 
def someFunc(){} 
}

object xyz {  }

}

private object extendabc {
val a = new abc.xyz()
a.someFunc()
}

You need to call this way because xyz is a nested member of the object abc . Look here .

Also please note you cannot define a apply method in the companion object of a case class as it provides the exact same apply() method (with the same signature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM