- 重写抽象类的抽象方法的时候不需要加override关键字,父类可以声明抽象字段(没有初始值的字段),重写这个抽象字段的时候也不要加上override关键字
- scala中的抽象类接口有traits的概念,可以理解成为java8中的带有默认实现的接口,traits是一个字段和行为的集合,通过with关键字一个类可以扩展多个traits,第一个混入的traits,要使用extends关键字,而不是traits关键字,with多个特质的时候,特质会被从做到右来进行构造,下面是一些exp
class Basic3 {
}
trait Logger{
def Log(msg : String): Unit ={
println("log " + msg)
}
}
trait LoggerAbs{
def Log(msg : String)
}
trait ConsoleLog extends LoggerAbs{
def Log(msg:String){
print("console log " + msg)
}
}
trait MessageLog extends ConsoleLog{
override def Log(msg:String): Unit ={
println("message log " + msg)
}
}
abstract class Account{
def save
}
class MyAccount extends Account with ConsoleLog{
def save(): Unit ={
println("i save money")
Log("100")
}
}
class Test2 extends ConsoleLog{
override def Log(msg : String): Unit ={
}
}
object Basic3 extends App{
var myacc2 = new MyAccount with MessageLog;
myacc2.save()
}
- object中的apply方法和类中的apply方法不太一样,类名加上()调用的是object的apply方法,而对象加括号调用的是类的object方法
class ApplyTest{
def apply() = "APPLY"
def test: Unit ={
println("test")
}
}
object ApplyTest{
var count = 0
def apply() = new ApplyTest
def static: Unit ={
println("static")
}
def incr ={
count += 1
}
}
object Basic4 extends App{
ApplyTest.static
var a = ApplyTest()
a.test
var t = new ApplyTest
println(t())
for (i <- 1 to 10){
ApplyTest.incr
}
println(ApplyTest.count)
}
- scala的包:支持嵌套,可串联,顶部标记,包可以在任何地方引入,其作用域一直到该语句所在的块的结尾,可以重命名引入成员,自动引入(java.lang._ scala. Predef._)
import java.util.{HashMap=>JavaHashMap} //重命名引入成员
package aa.bb.cc.dd
class xxx{
private[dd] def sample() = {} //只能被dd作用域下面的成员所引用
}
- 等等其他特性
class Basic5 {
}
//case class:
// 1.构造函数中每个变量都是val不建议用var
// 2. 不用new就可以产生对象(apply方法)
case class Book(name : String, author : String, price : Int)
object Basic5 extends App{
val value = 3
//match语句实际上类似于其他语言中的switch语句,而且没有break语句,因为他匹配完成后会立即退出
var result = value match {
case 1 => "one"
case 2 => "two"
case _ => "some other values" //_代表通配符,类似其他语言的default语句
}
var result2 = value match {
case i if i == 1 => "one"
case i if i == 2 => "two"
case _ => "three"
}
println("result of match is " + result)
println("result of match is " + result2)
def t(t : Any) = t match {
case t:Int => println("Int")
case t:String => println("String")
case _ => println("unknown type")
}
t("orange")
var macTalk = Book("MacTalk", "WangCheng", 22)
macTalk match {
case Book(name, author, price) => println("This is a Book")
case _ => println("Unknown Type")
}
//常用的高阶函数map,filter,reduce
val l = List(1,2,3,4,5,6,7,8)
val mapList = l.map((x) => 2 * x)
val mapList2 = l.map(2 * _)//占位符方式,不用x
//元组
val hostPort = ("localhost", "8080")
println(hostPort _1)//注意下标从1开始
println(hostPort _2)
//map
val m = Map("a" -> "b")
m("a")
//getOrElse语句实际上类似于,if(xx != null)这种
println(m.get("a").getOrElse())
println(m.get("b").getOrElse("None"))
//foreach
//无返回值,只会产生一些副作用
mapList.foreach((x : Int) => println(3 * x))
//filter
val mapListFiltered1 = l.filter(_ % 2 == 0)
val mapListFiltered2 = l.filter(_ % 2 == 1)
println(mapListFiltered1)
println(mapListFiltered2)
//zip,配对
val zipList = mapListFiltered1.zip(mapListFiltered2)
//zip以zip调用者为准
println(zipList)
//pritition
val pList = l.partition(_%2 == 0)
println(pList)
//flatten
val lists = List(List("a", "b", "c"), List("1","2","3"),List(1))
val fList = lists.flatten
println(fList)
//flatMap
val fmList = lists.flatMap(x => x.map(_ + "2"))
println(fmList)
//泛型
//隐式转换
//1.位于原目标类型伴生对象中的隐式函数
//2.位于当前作用于可以以单个标识符指代的隐式函数
class Basic5{
}
class A{
}
class RichA(a : A){
def rich{
println("rich !")
}
}
object Basic6 extends App{
implicit def a2RichA(a : A) = new RichA(a)
val a = new A
a.rich
}
def testParam(implicit name : String): Unit ={
println(name)
}
implicit val name = "implicit!!!"
testParam
implicit class Calculator(x:Int){
def add(a:Int) = a + 1
}
println(1.add(1))
//注意,需要用到哪个隐式转换,一定需要将其引过来
}