您的位置:首页 > 其它

scala学习笔记(高级特性1)

2016-02-01 10:45 495 查看
一、一切皆为对象

对于基础篇中写道的在scala中一切皆为对象或许大家理解还是不很清楚,举个例子:

在scala中没有运算符的概念,相应的是方法的概念。

1+2


被看成是调用1的带有2变量的+方法,即1.+(2),

for(i<- 1 to 3)
print(i)
等价于
for(i<- 1.to(3))
print(i)


二、伴生对象

在scala中没有static变量,而为了弥补这一空缺,scala存在着伴生对象的概念,即在伴生对象中定义的方法可以在类中直接调用。

class Student(var name:String,var age:Int){
}
object Student{
def apply( pname:String, page:Int)=new Student(pname,page)

}

var s = Student("zhangsan",15)
print(s.name)


解析:apply是Student类的伴生对象中的方法,在调用Student类初始化时,会调用派生对象中的apply方法,给Student类赋值,避免使用new对象了。

对于apply方法:生成新对象时自动掉用。

三、高级特性

1.Array

赋值:

var arrays:Array[String] = new Array[String](2)
arrays(0)="abc"
等价于
arrays(0).update("abc")

var arr = Array("a","b","c")
等价于
var arr = Array.apply("a","b","c")


2.list

val oneTwo = List(1, 2)
val threeFour = List(3, 4)
val oneTwoThreeFour = oneTwo ::: threeFour
print(oneTwoThreeFour)


val oneTwoThree = 1 :: 2 :: 3 :: Nil
println(oneTwoThree)


list操作方法

名称作用
List() or NilThe empty List
List(“Cool”, “tools”, “rule”)Creates a new List[String] with the three values “Cool”, “tools”, and “rule”
val thrill = “Will” :: “fill” ::”until” :: NilCreates a new List[String] with the three values “Will”, “fill”, and”until”
List(“a”, “b”) ::: List(“c”, “d”)Concatenates two lists (returns a new List[String] with values “a”, “b”,”c”, and “d”)
thrill(2)Returns the element at index 2 (zero based) of the thrill list (returns “until”)
thrill.count(s => s.length == 4)Counts the number of string elements in thrill that have length 4 (returns 2)
thrill.drop(2)Returns the thrill list without its first 2 elements (returns List(“until”))
thrill.dropRight(2)Returns the thrill list without its rightmost 2 elements (returns List(“Will”))
thrill.exists(s => s == “until”)Determines whether a string element exists in thrill that has the value “until” (returns true)
thrill.filter(s => s.length == 4)Returns a list of all elements, in order, of the thrill list that have length 4 (returns List(“Will”, “fill”))
thrill.forall(s =>s.endsWith(“l”))Indicates whether all elements in the thrill list end with the letter “l” (returns true)
thrill.foreach(s => print(s))Executes the print statement on each of the strings in the thrill list (prints “Willfilluntil”)
3.tuples

val pair = (99, "Luftballons")
println(pair._1)
println(pair._2)


4.sets和maps

set包含:

scala.collection.mutable.Set;

scala.collection.immutable.HashSet;

import scala.collection.mutable.Set
val movieSet = Set("Hitch", "Poltergeist")
movieSet += "Shrek"
println(movieSet)


maps包含:

scala.collection.mutable.Map

import scala.collection.mutable.Map
val treasureMap = Map[Int, String]()
treasureMap += (1 -> "Go to island.")
treasureMap += (2 -> "Find big X on ground.")
treasureMap += (3 -> "Dig.")
println(treasureMap(2))

或

val romanNumeral = Map(
1 -> "I", 2 -> "II", 3 -> "III", 4 -> "IV", 5 -> "V"
)
println(romanNumeral(4))


5.File读取

var longestLine = lines.reduceLeft((a,b) => if(a.length >b.length) a else b)
def maxWidth = widthOfLength(longestLine)
def widthOfLength(s:String)=s.length.toString.length

for (line <- lines) {
val numSpaces = maxWidth - widthOfLength(line)
val padding = " " * numSpaces
println(padding + line.length +" | "+ line)
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: