一:scala单例
package com.miaoli.day04
/**
* desc: scala 的obje类型
*
* 在Scala中没有静态方法和静态字段,但是可以使用object这个语法结构来达到同样的目的
* 1.存放工具方法和常量
* 2.高效共享单个不可变的实例
* 3.单例模式
* Object是class的一个单例,其里面定义的都是静态的成员变量和方法。
* Object本身是单例而且静态的。在第一次调用Object的方法的时候,会调用方法和代码块的代码,
* 之后在调用的时候,就不会执行构造器里面的代码
*/
object O1 {
val age = 18
}
object Demo01Singleton {
//object 所有属性和方法 都是静态的
val name = "name1"
def method: Unit = {
println("this is method")
}
def main(args: Array[String]): Unit = {
val o1 = Demo01Singleton
val o2 = Demo01Singleton
println(o2.hashCode())
println(o1.hashCode())
println(name)
method
val o3=o1
val o4=o2
println(o4.hashCode())
println(o3.hashCode())
println(O1.age)
}
}
二:APP 程序临时入口
package com.miaoli.day04
object Demo02App extends App {
//App是一个trait 特质 可以代替 main作为程序入口 多用于调试
//def main(args: Array[String]): Unit = {
// println("hello world")
//
//}
println("hello,world")
}
三:apply源码
package com.miaoli.day04
object Demo03Apply {
def main(args: Array[String]): Unit = {
//使用apply 的方式创建数组 不需要new 直接可以使用
val arr=Array(1,2,3,4)
val list=List(1,2,3)
// /** Creates an array of `Int` objects */
// // Subject to a compiler optimization in Cleanup, see above.
// def apply(x: Int, xs: Int*): Array[Int] = {
// val array = new Array[Int](xs.length + 1)
// array(0) = x
// var i = 1
// for (x <- xs.iterator) { array(i) = x; i += 1 }
// array
// }
}
}
四:自定义apply
package com.miaoli.day04
//自定义Apply
//伴生类 主构造器
class Room {
var size = 110
var floor = 17
//辅助构造器
def this(size: Int) {
this() //辅助构造器第一句必须要调用主构造器
this.size = size
}
def this(size: Int, floor: Int) {
this() //辅助构造器第一句必须要调用主构造器
this.size = size
this.floor = floor
}
}
//自定义apply 在伴生对象定义apply方法 返回对象
object Room {
def apply(): Room = {
new Room()
}
def apply(size: Int): Room = {
new Room()
}
def apply(size: Int, floor: Int): Room = {
new Room(size, floor)
}
}
object Demo04DefindApply {
def main(args: Array[String]): Unit = {
val room = new Room()
val room1 = Room() //使用apply的方式创建Room对象
val room2 = new Room(100)
val room3 = Room(100)
val room4 = new Room(100, 17)
val room5 = Room(100, 17)
}
}
五:枚举
package com.miaoli.day04
//枚举 继承 Enumeration
object Demo05Enum extends Enumeration {
val HADOOP = Value(1, "hadoop")
val SPARK = Value(2, "spark")
val SCALA = Value(3, "scala")
val FLINK = Value(4, "flink")
def main(args: Array[String]): Unit = {
println(HADOOP)
println(Value(6)) //<Invalid enum: no field for #6>
println(Demo05Enum.withName("spark"))
println(Demo05Enum.values)
//Demo05Enum.ValueSet(hadoop, spark, scala, flink, <Invalid enum: no field for #6>)
println(Demo05Enum.SPARK)//最常用
println(Demo05Enum.apply(2))
}
}
六:异常处理
package com.miaoli.day04
//异常处理 !#抛抛抛啊 哈哈哈
/**
*
* 异常捕捉的机制与其他语言中一样,如果有异常发生,catch字句是按次序捕捉的。
* 因此,在catch字句中,越具体的异常越要靠前,越普遍的异常越靠后。
* 如果抛出的异常不在catch字句中,该异常则无法处理,会被升级到调用者处。
* 捕捉异常的catch子句,语法与其他语言中不太一样。
* 在Scala里,借用了模式匹配的思想来做异常的匹配,
* 因此,在catch的代码里,是一系列case字句
*/
//import java.io.FileNotFoundException
import java.io.FileNotFoundException
import scala.io.Source
object Demo06Exception {
def main(args: Array[String]): Unit = {
try {
Source.fromFile("N://plz.txt")
} catch {
//模式匹配
case e: IndexOutOfBoundsException => println("IndexOutOfBoundsException")
case e: FileNotFoundException => println("FileNotFoundException")
case e: Exception => println("Exception")
} finally {
println("无论有没有异常,都会被执行")
}
}
}
七:call-by-name和call-by-value
package com.miaoli.day04
/**
* call-by-name和call-by-value
* scala的函数参数有两种情况,一种为call-by-value一种为call-by-name
*
* def 和 val的关系其实就是call-by-name和call-by-value的关系,
* def对应的是by-name,val对应的是by-value
*/
object Demo07CallByName_Value {
def main(args: Array[String]): Unit = {
println(callByValue(10, func()))
println(callByName(10, func))
}
val func = () => {
println("func") // 副作用
10
}
//传函数名称
def callByName(a: Int, func: () => Int): Int = {
println("callByName")
a + func()
}
//传函数返回值
def callByValue(a: Int, func: Int): Int = {
println("callByValue")
a + func
}
}