2nd: Scala Object-oriented thorough mastery and spark source reading

Source: Internet
Author: User

Defining classes
Class Hiscala {
private var name = "Spark"
Def sayname () {println (name)}
def getname=name
}

Instantiating classes
scala> val scal = new Hiscala
Scal:hiscala = [email protected]

Methods to invoke a class
Scala> Scal.sayname ()
Spark

Scala> Scal.getname
res6:string = Spark


Class Hiscala {
var name = "Scala"
Def sayname () {println (name)}
def getname=name
}

Access to public getter,setter
Scala> println (Scal.name)
Scala

Scala> Scal.name
res9:string = Scala

Overriding the default getter and setter note that the _= is connected together
Class Person {
private var myName = "Flink"
def name = This.myname
def name_= (newname:string) {
Myname=newname
println ("Hi:" + myName)
}
}

Scala> Rocky.name
res10:string = Flink

scala> rocky.name= "Spark"
Hi:spark
rocky.name:String = Spark


Private[this] is represented as private class, object private, can only be used by the object of the class, cannot be used by methods of the current class
Class Person {
private var myName = "Flink"
def name = This.myname
def name_= (newname:string) {
Myname=newname
println ("Hi:" + myName)
}
Def talk (P:person) = {
println ("Hello" +p.name)
}
}

val p1 = new person
val P2 = new person
P2.name= "Spark"

Scala> P1.talk (p2)
Hello Spark

However, after the following modifications, name is not a member of the person, only the object access
Class Person {
Private[this] var name = "Flink"
Def talk (P:person) = {
println ("Hello" +p.name)
}
}

Private[package-name] ...
That is, all classes and objects in the Package-name package can access the members behind private.

constructor function
Class Person {
var name = "Flink"
var age = 18
def this (name:string) {
This ()
This.name=name
}
def this (name:string,age:int) {
This (name)
This.age=age
}
}


The default constructor, such as Class Sparkcontext (config:sparkconf), is put together with the name

Class Person {
println ("Big Data")//default constructor will execute all code blocks that are not placed in the method
var name = "Flink"
var age = 18
def this (name:string) {
This ()
This.name=name
}
def this (name:string,age:int) {
This (name)
This.age=age
}
}

Scala> val p = new Person ("Spark", 20)
Big Data
P:person = [email protected]


Object associated objects, must be in the same file

Class Person {
var name = "Flink"
var age = 18
def this (name:string) {
This ()
This.name=name
}
def this (name:string,age:int) {
This (name)
This.age=age
}
}

For storing constants and static methods
Object Person {
println ("Scala")//Call only once
var salary = 0.0
def getsalary = Salary
}


Scala> person.getsalary
Scala
res16:double = 0.0

Scala> person.getsalary
res17:double = 0.0

Abstract class
Interface trait

Homework: Analyze the two classes of Sparkcontext and rdd yourself

2nd: Scala Object-oriented thorough mastery and spark source reading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.