Scala入門與進階(四)- Scala面向對象

4. Scala面向對象

1.面向對象概述

  • 封裝:屬性方法封裝到類中java

  • 繼承:父類和子類直接的關係bash

  • 多態:***** 父類引用指向子類對象 精髓所在,開發框架的基石app

2.類的定義和使用

package com.gwf.scala.course04

object SimpleObjectApp {

def main(args: Array[String]): Unit = {

val person = new People()
person.name = "Messi"
println(person.name+" "+person.age)
println("invoke eat method "+person.eat)
person.watchFootball("Barcelona")
person.printInfo()
}

class People{
var name:String = _ //_表明佔位符
val age = 10

// 私有變量,外部不能訪問,[]裏的this表明訪問權限,能夠填當前所在包,則包內能夠訪問
private [this] var gender:String = "male"

def printInfo(): Unit ={
println(gender)
}

def eat():String={
name + "eat ....."
}

def watchFootball(teamName: String)={
println(name+"is watching match of "+teamName)
}

}
}
複製代碼
scala> var d:Double = _
d: Double = 0.0

scala> val i:Int = _
<console>:11: error: unbound placeholder parameter
val i:Int = _
^

scala> var i:Int = _
i: Int = 0

scala> var s:String = _
s: String = null

scala>
複製代碼

3.主構造器和附屬構造器

// 主構造器,若是不加val/var修飾符則默認是private[this] val 類型
class Person(val name:String,val age:Int,other:String){
println("Person Constructor enter...")
val school = "ustc"
var gender:String = _

def getOther(): String = other

def this(name:String,age:Int,other:String,gender:String){
this(name,age,other) // 附屬構造器的第一行必須調用主構造器或者其餘構造器
this.gender = gender
}
println("Person Constructor leave...")

}
複製代碼

4.繼承

// 子類繼承父類,父類的屬性在子類構造函數中能夠不加val/var聲明,子類特有的屬性必需要加,不然也會變成private[this] val
class Student(name:String,age:Int,other:String,val school:String) extends Person(name,age,other){

}
複製代碼

5.重寫

class Student(name:String,age:Int,other:String,val school:String) extends Person(name,age,other){

// 重寫必須加override
override val country: String = "USA"

// $表明this
override def toString = s"Student($country, $school)"
}
複製代碼

6.抽象類

/** * 類的一個或者多個方法沒有完整的實現(只有定義,沒有實現) */
abstract class Person2{
def speak

var name:String

var age:Int
}

/** * 普通了繼承抽象類要實現未實現的抽象方法和抽象屬性 */
class Student2 extends Person2{
override def speak: Unit = println("speak")

override var name: String = _
override var age: Int = _
}
複製代碼

7.伴生類和伴生對象

/** * 若是有一個class,還有一個與class同名的object * 那麼就稱稱這個個object是class的伴生対象, class是object的伴生類,二者相輔相成 */
class ApplyTest{

}

object ApplyTest{

}
複製代碼

8.Apply 方法

package com.gwf.scala.course04

object ApplyApp {
def main(args: Array[String]): Unit = {
for(i <- 1 to 10){
ApplyTest.incr
}

println("count:"+ApplyTest.count) // 10 說明object自己就是一個單例對象

println("~~~~~~~~~~~~~")
val b = ApplyTest() // ==>Object.

println("~~~~~~~~~~~~~")
val c = new ApplyTest()
println(c)
c() // ==>Class.

// 類名() ===> Object.apply
// 對象() ===> Class.apply
}
}

/** * 若是有一個class,還有一個與class同名的object * 那麼就稱稱這個個object是class的伴生対象, class是object的伴生類,二者相輔相成 */
class ApplyTest{
def apply()= {
println("Class ApplyTest apply...")
}
}

object ApplyTest{

println("Object ApplyTest enter...")

var count = 0

def incr = {
count = count + 1
}

// 最佳實踐:在0bject的apply方法中去new Class
def apply()= {
println("Object ApplyTest apply...")
}

println("Object ApplyTest leave...")

}
複製代碼

9.case class

package com.gwf.scala.course04

// 一般用到模式匹配裏
object CaseClassApp {
def main(args: Array[String]): Unit = {
println(Dog("wangcai").name)
}
}

case class Dog(name:String) 複製代碼

10.Trait

Trait 相似於java的接口,可是能夠集成抽象類,並實現其抽象方法。框架

// Triat多集成:XXX extends ATrait with BTrait
class SparkConf(loadDefaults: Boolean) extends Cloneable with Logging with Serializable {

複製代碼
相關文章
相關標籤/搜索