Today we learned the implementation of chained invocation styles in Scala, and in spark programming we often see the following code:
Sc.textfile ("hdfs://..."). FlatMap (_.split ("")). Map (_,1). Reducebykey (_ + _) ...
This style of programming is called chained invocation, and its implementation is described in the following code:
Class Animal {def Breathe:this.type = this}
Class Cat extends Animal {def eat:this.type = this}
Object test51{
def main (args:array[string]) {
Val cat = new Cat
Cat.breathe.eat
}
}
In the above code, we can see that our cat instance called the Breathe method and then called the Eat method.
If we make some changes to the animal class and cat classes, as shown here:
Class Animal {def breathe = this}
Class Cat extends Animal {def eat = this}
Object test51{
def main (args:array[string]) {
Val cat = new Cat
Cat.breathe.eat
}
}
This way, when we call Cat.breathe.eat, the compiler will error
It seems that the crux of the problem is the this.type of the method definition = this is here.
By applying this notation, we can make a chain call method and invoke the method after the class instantiation.
Share more of the Scala resources:
Baidu Cloud Disk: http://pan.baidu.com/s/1gd7133t
Micro Cloud Disk: http://share.weiyun.com/047efd6cc76d6c0cb21605cfaa88c416
360 Cloud Disk: Http://yunpan.cn/cQN9gvcKXe26M (extract code: 13CD)
Information from DT Big Data Dream Factory public account: Dt_spark
Follow the account for more information about Scala learning
51st: The implementation code of the chain call style in Scala and its extensive application in spark programming