學習scala語言的硬性記錄

這個是我自己的學習筆記,其他人不要看

 

學習scala的機械式記憶

1.創建main方法的類是object的類
2.val name:Leising = aaa
3.def sum(n1:Leixing,n2:Leixing):Leixing={
return n1+n2
}
4.var和val來定義變量,var=variable可變的,val=value不可變的
5.dog:Dog = new Dog 這裏不需要括號
6.沒有基礎數據類型,都是對象;AnyVal和AnyRef都是對象
7.Unit Null Nothing
8.強制類型轉換 (2.3).toInt
9.沒有++ --的操作
10.沒有三目運算符
11.控制檯輸入: StdIn.readLine()
StdIn.readInt()
StdIn.readDouble()
12.沒有switch
13.for(i <- 1 to 3){} 這個是i從1到3的循環 不能用 in
1 to 3 是(1,2,3)
1 unit 3 是(1,2)
range(1,10) 和 1 unit 10 相同返回(1,2,3,4,5,6,7,8,9)
14.沒有continue和break
有break()和breakable,需要引入util.control.Breaks._
用循環守衛或者if else實現continue功能
15.循環返回值用 yield
16.def f1(op:=>Unit):Unit{
op
}
表示f1是一個函數,傳入的是一個函數op。 op就是執行這個函數
17. 將一個函數賦予一個變量中
var f1 = dog.sum _
var f2 = (n1:Int,n2:Int)=>{n1+n2}
18.聲明一個函數
def f1(n1:Int,n2:Int):Int = {

}
19.惰性函數
lazy 修飾val變量
lazy不能修飾var變量
20.異常捕獲
try{
}catch{
}finally{
}

catch{
case ex:Exception=>{
}
}
21.聲明某個方法可以拋異常
@throws(classOf[NumberFormatException])
def f1()={
"abc".toInt
}
22.scala的類默認就是public
23.某個字段如果賦值爲null,則必須添加類型;某個字段如果賦值爲_,則必須添加類型
24.構造器 主構造器和輔助構造器;輔助構造器需要調用主構造器
class Person(pname:String,page:Int){
var name = pname
var age = page

def this(name:String){
this(name,0 )
}
}
25.註解@BeanProperty可以自動生成get和set方法
26.包對象 package object scala2
那麼在scala2的類中都可以使用包對象定義的屬性和方法
27.伴生類和伴生對象
伴生對象,可以訪問伴生類的所有方法和屬性
伴生對象,相當於定義的全都是靜態的屬性和方法
伴生類和伴生對象需要放在同一個文件中,且名字需要相同
伴生對象的apply方法生成對象
28.isInstanceOf asInstanceOf classOf
判斷是不是某類,強轉成某類,獲得類的名字
29. trait 特質
class T1 extends T0 with T2 with T3 with T4 先構造T0再T2再T3再T4再T1
trait特質可以有抽象方法和具體實現
new T1 with T2 with T3 with T4 先T1再T2再T3再T4
30. 集合
immutable 不可變
mutable 可變
1.
定長數組是Array
邊長數組是ArrayBuffer
toArray toBuffer是轉換成定長,轉換成邊長
2.
Array.ofDim[Double](3,4) 創建 多維數組
3.
scala的數組和java的數組之間的轉化
var arrList = new ProcessBuilder(arr).command()
4.List 不可變集合
31.元組
val tuple = (1,2,"hello")
for(i <- tuple.productIterator){

}
32. List 不可變list
ListBuffer 可變list
::運算符和:::運算符
33. list的map、flatMap、filter、reduceLeft、reduceRight、foldLeft、foldRight、scanLeft、scanRight、zip
collect(偏函數)

34. 11章沒看

35.match case 模式匹配
oper match{
case '+'=>{println()}
case '-'=>{println()}
case _ if "a".equals("a"){}
case _=>{println()}
}

36.偏函數 partialFunction 裏面需要重寫兩個方法 isDefinedAt 和 apply

37.匿名函數 val tro = (x:Int)=>{x+1}

38.函數作爲入參
def f1(f:(Int,Int)=>Int,x1:Int,x2:Int){
f(x1,x2)
}

39. 併發編程模型AKKA

40.17章的泛型
[]    [_<: A]    [_>:A]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章