1. scala初识

spark由scala编写,要解析scala,首先要对scala有基本的了解。

1.1 class vs object

A class is a blueprint for objects. Once you define a class, you can create objects from the class blueprint with the keyword new.

import java.io._

class Point(val xc: Int, val yc: Int) {
var x: Int = xc
var y: Int = yc
def move(dx: Int, dy: Int) {
x = x + dx
y = y + dy
println ("Point x location : " + x);
println ("Point y location : " + y);
}
} object Test {
def main(args: Array[String]) {
val pt = new Point(10, 20); // Move to a new location
pt.move(10, 10);
}
}

类的继承,使用extend实现:

import java.io._

class Point(val xc: Int, val yc: Int) {
var x: Int = xc
var y: Int = yc
def move(dx: Int, dy: Int) {
x = x + dx
y = y + dy
println ("Point x location : " + x);
println ("Point y location : " + y);
}
} class Location(override val xc: Int, override val yc: Int,
val zc :Int) extends Point(xc, yc){
var z: Int = zc def move(dx: Int, dy: Int, dz: Int) {
x = x + dx
y = y + dy
z = z + dz
println ("Point x location : " + x);
println ("Point y location : " + y);
println ("Point z location : " + z);
}
} object Test {
def main(args: Array[String]) {
val loc = new Location(10, 20, 15); // Move to a new location
loc.move(10, 10, 5);
}
}

单例对象

  Scala is more object-oriented than Java because in Scala we cannot have static members. Instead, Scala has singleton objects. A singleton is a class that can have only one instance, i.e., object. You create singleton using the keywordobject instead of class keyword. Since you can't instantiate a singleton object, you can't pass parameters to the primary constructor. You already have seen all the examples using singleton objects where you called Scala's main method. Following is the same example of showing singleton:

import java.io._

class Point(val xc: Int, val yc: Int) {
var x: Int = xc
var y: Int = yc
def move(dx: Int, dy: Int) {
x = x + dx
y = y + dy
}
} object Test {
def main(args: Array[String]) {
val point = new Point(10, 20)
printPoint def printPoint{
println ("Point x location : " + point.x);
println ("Point y location : " + point.y);
}
}
}

1.2 trait 

A trait encapsulates method and field definitions, which can then be reused by mixing them into classes. Unlike class inheritance, in which each class must inherit from just one superclass, a class can mix in any number of traits.

Traits are used to define object types by specifying the signature of the supported methods. Scala also allows traits to be partially implemented but traits may not have constructor parameters.

a trait is very similar to what we have abstract classes in Java. Below is a complete example to show the concept of traits:

trait Equal {
def isEqual(x: Any): Boolean
def isNotEqual(x: Any): Boolean = !isEqual(x)
} class Point(xc: Int, yc: Int) extends Equal {
var x: Int = xc
var y: Int = yc
def isEqual(obj: Any) =
obj.isInstanceOf[Point] &&
obj.asInstanceOf[Point].x == x
} object Test {
def main(args: Array[String]) {
val p1 = new Point(2, 3)
val p2 = new Point(2, 4)
val p3 = new Point(3, 3) println(p1.isNotEqual(p2))
println(p1.isNotEqual(p3))
println(p1.isNotEqual(2))
}
}

1.3 extractor 

An extractor in Scala is an object that has a method called unapply as one of its members. The purpose of that unapply method is to match a value and take it apart. Often, the extractor object also defines a dual method apply for building values, but this is not required.

Following example shows an extractor object for email addresses:

object Test {
def main(args: Array[String]) { println ("Apply method : " + apply("Zara", "gmail.com"));
println ("Unapply method : " + unapply("Zara@gmail.com"));
println ("Unapply method : " + unapply("Zara Ali")); }
// The injection method (optional)
def apply(user: String, domain: String) = {
user +"@"+ domain
} // The extraction method (mandatory)
def unapply(str: String): Option[(String, String)] = {
val parts = str split "@"
if (parts.length == 2){
Some(parts(0), parts(1))
}else{
None
}
}
}

1.4 closure 

closure is a function, whose return value depends on the value of one or more variables declared outside this function.

object Test {
def main(args: Array[String]) {
println( "muliplier(1) value = " + multiplier(1) )
println( "muliplier(2) value = " + multiplier(2) )
}
var factor = 3
val multiplier = (i:Int) => i * factor
}

i, is a formal parameter to the function. Hence, it is bound to a new value each time multiplier is called.

 1.5 function 

  A function is a group of statements that together perform a task. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically, the division usually is so that each function performs a specific task.

  Scala has both functions and methods and we use the terms method and function interchangeably with a minor difference. A Scala method is a part of a class which has a name, a signature, optionally some annotations, and some bytecode where as a function in Scala is a complete object which can be assigned to a variable. In other words, a function, which is defined as a member of some object, is called a method.

  A function definition can appear anywhere in a source file and Scala permits nested function definitions, that is, function definitions inside other function definitions. Most important point to note is that Scala function's name can have characters like +, ++, ~, &,-, -- , \, /, : etc.

object Test {
def main(args: Array[String]) {
println( "Returned Value : " + addInt(5,7) );
}
def addInt( a:Int, b:Int ) : Int = {
var sum:Int = 0
sum = a + b return sum
}
}

参考文献:

【1】http://www.tutorialspoint.com/scala/scala_classes_objects.htm

【2】http://www.tutorialspoint.com/scala/scala_traits.htm

【3】http://www.tutorialspoint.com/scala/scala_extractors.htm

【4】http://www.tutorialspoint.com/scala/scala_functions.htm

【5】http://www.tutorialspoint.com/scala/scala_closures.htm

最新文章

  1. 使用Ninject进行DI(依赖注入)
  2. tyvj 1055 区间dp
  3. Mysql JDBC Url参数说明useUnicode=true&characterEncoding=UTF-8
  4. iOS 开发学习35 本地化
  5. java反射拼接方法名动态执行方法
  6. 【★】RSA-什么是不对称加密算法?
  7. 深入理解计算机系统_3e 第五章家庭作业 CS:APP3e chapter 5 homework
  8. LODOP打印超过后隐藏内容样式
  9. 安装vue错误详情解决办法
  10. ssl握手数据结构
  11. Beta阶段冲刺---Day2
  12. hdu-1036(格式题+精确度)
  13. [基础知识]row类visible使用
  14. keep or remove data frame columns in R
  15. 雷林鹏分享:Ruby 哈希(Hash)
  16. CMD命令进入文件夹
  17. php快速获取所有的自定义常量用户常量
  18. sparkSQL——DataFrame&Datasets
  19. freeradius连接mysql数据库慢
  20. android 文件上传,中文utf-8编码

热门文章

  1. [UnityUI]循环滑动列表
  2. 误操作 rpm -e --nodeps zlib
  3. 值得学习的html知识
  4. Fragment-两种使用方式
  5. 【DRF认证】
  6. mktemp---创建暂存文件
  7. cksum---检验文件CRC是否正确
  8. Install Docker Mac OS X
  9. android基于开源网络框架asychhttpclient,二次封装为通用网络请求组件
  10. hdoj-1289-A Bug's Life【种类并查集】