I am understanding my existing project, few things I am not able to understand:
trait PeriodA {
def start: Long
def stop: Long
def description: String
def manageTo: String
}
case class PeriodEntity(
start: Long,
stop: Long,
description: String,
manageTo: String)
extends PeriodA
Could you please explain what is happening here?
Step by step:
trait PeriodA {
– definition of a trait (which is roughly an interface – for further understanding please read spec )
def start: Long def stop: Long def description: String def manageTo: String
– definition of 'on-call' evaluated properties of trait. You could consider them as Java getFieldName
functions.
case class PeriodEntity
definition of a 'case class' which is slightly more complicated than general one. In few words, that definition makes compiler implicitly add val
prefix to each so-called 'element' (field) of the definition. It also define the companion object with apply/unapply
functions pair, which comes in handy in case you want to use pattern matching, but that's out of the current topic. Case classes specification
start: Long, stop: Long, description: String, manageTo: String
– definition of both constructor parameters and class fields, which called 'elements'. As far as compiler implicitly mark elements with val
prefix, they should be considered as immutable. As it was mentioned previously, they could be considered as get accessors.
extends PeriodA
- declares that class PeriodEntity
mixins (implements) PeriodA
interface. As far as case class elements are implicitly considered as val's by compiler there is no need to explicitly implement or override abstract PeriodA
methods – compiler will treat elements as a default implementations for you. Thanks for making me understood, I like to share my understanding,
trait PreridA
- is nothing but a interface have 4 get methods.
case class PeriodEntity extends PeriodA
-- PeriodEntity should have 4 get methods as mentioned in PeriodA. If you delete any filed in PeriodEntity then you will get compilation exception. In simple words, it is like a class implements an interface.
I got solution for these type of misunderstandings. For this you should have basic knowledge on Java.
Or, cd <..>\\target\\scala-2.11\\classes Run
javap package.Employee
You will see java code in console.
1,2 steps needed only if you working on real-time projects, actual project code includes lot of logic. If you feel you can understand then you can omit 1 and 2 steps.
So, then you can easily understand what exact meaning of your Scala code.
It looks big procedure, but it won't take much time. And make it understand yourself instead of somebody explained.
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.