今日学习:scala,英语
在scala中,创建的类默认是public类型
package com.chapter01.classabout object Demo01 { def main(args: Array[String]): Unit = { var n = new N println(n.v1) println(n.v2) println(n.v3) println(n.v4) } } class Person { var A: Int = 10 var B = 8099.9 //省略类型时,会根据赋值自动推导 var Name = null var address: String = null } class N { //使用_赋值必须声明类型,否则报错 var v1: String = _ //null var v2: Byte = _ //0 var v3: Double = _ //0.0 var v4: Boolean = _ //false }
scala中多态的用法需要提前声明类型
package com.chapter01.classabout object CreateObj { def main(args: Array[String]): Unit = { var n1 = new P2 //类型是P2 var n2: P1 = new P2 //类型是P1 } } class P1 { } class P2 extends P1 { }
scala中关于foreach的一些用法:
package com.chapter01.exercise object exercise01 { def main(args: Array[String]): Unit = { for (i <- (0 to 10).reverse) { print(i + " ") } println() println("等价于:") var list = List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) println(list.reverse) println("等价于:") //foreach函数(f:Int=>U) (0 to 10).reverse.foreach(showit) //(0 to 10).reverse.foreach(println) println() println("完成字符串Hello乘积:") println("使用foreach:") var res = 1L "Hello".foreach(res *= _.toLong) println("res=" + res) println("使用函数:") val str = "Hello" //取第一个字符 var substr1 = str.take(1) //取除第一个之外的所有字符 var substr2 = str.drop(1) println(substr1 + " | " + substr2) println("res=" + product("Hello")) } def showit(n: Int): Unit = { print(n + " ") } def product(s: String): Long = { if (s.length == 1) return s.charAt(0).toLong else s.take(1).charAt(0).toLong * product(s.drop(1)) } }