Scala學習 2 Scala物件導向

土豆拍死馬鈴薯發表於2017-10-05
object Scala_study3 {
  def main(args: Array[String]): Unit = {

    /*
    * 2.1 Scala面對物件詳解
    *
    * 2.1.1 Scalaclassobject初介紹
    * scalaclass的定義與java類似,區別是在Scala,類名不是必須和檔名相同,且一個檔案中可以定義多個class.
    * Scala中的class不存在靜態成員,Java中的靜態成員由Scala中的object物件替代,
    * object物件名與class名相同時稱objectclass[伴生物件]
    * */

    //2-1 Scala類的定義實戰
    class HiScala{
      private val name:String = "zhangsan"
      def sayName(): Unit ={
        println(name)
      }
      def getName = name
    }

    val hiScala = new HiScala
    hiScala.sayName()
    println(hiScala.getName)


    //2-2 object定義實戰
    object Person{
      println("建構函式呼叫一次")
      val age = 10
      def getAge = age
    }
    Person.getAge
    Person.getAge

    //2-3 繼承

    abstract class Animal{
      def eat = {
        println("Eat food !!!")
      }
      def run
    }

    class Cat extends Animal{
      override def eat: Unit = {
        println("Eat mouse!!!")
      }

      override def run: Unit = {
        println("cat is running...")
      }

    }

    class Dog extends Animal{
      override def run: Unit = {
        println("dog is running")
      }
    }

    val c = new Cat
    c.eat
    c.run
    val d = new Dog
    d.eat
    d.run
  }

  /*
  * Eat mouse!!!
    cat is running...
    Eat food !!!
    dog is running
  * */

  //2-4 子類不能重寫父類中被final修飾的方法和屬性

  /*
  * 2.1.2 主構造器和輔助構造器
  * */

  //2-5 Scala預設構造器在類上
  class Person1{

  }//預設無參構造器

  class Person2(name:String){
    println(name)
  }//預設帶一個字串引數的構造器

  //2-6 構造器過載(輔助構造器)
  class Person3(name:String){
    println(name)
    def this(name:String,age:Int)
    {
      this(name)
      println(name + ":" + age)
    }
    def this(name:String,age:Int,sex:String)
    {
      this(name,age)
      println(name + ":" + age + ":" + sex)
    }
  }

  //2-7 構造器過載(多引數預設構造器)
  class Student1(name:String,age:Int,sex:String){
    println(name + ":" + age + ":" + sex)
  }
  val s = new Student1("chenjie",23,"male")


  /*
  * 2.1.3 類的欄位和方法徹底精通
  * */
  class Person4{
    var name : String = "zhangsan"
    var age = 30
    val sex = "famale"
    def seyHi = println("Hi!!!")
    def increase(age:Int):Int = this.age + age
  }

  val p = new Person4
  println(p.name)
  println(p.sex)
  p.name = "lisi"


  //2-9 複寫setter
  class Person5{
    private var myName = "Flink"
    def name = this.myName
    def name_=(newName:String){//等號兩邊不能有空格
      myName = newName
      println("hi:" + myName)
    }
  }

  //2-10 自定義setter
  class Person6{
    private var myName = "Flink"
    def name = this.myName
    def update(newName:String): Unit ={
      myName = newName
    }
  }


  //2-11 private[this]實戰,用此修飾的欄位或方法只能被當前物件訪問,當前物件的其他例項無法訪問.




  /*
  * 2.1.4 抽象類 介面的實戰詳解
  * */

  abstract class Person7(name:String){
    println(this.name)
    private var age = 20
    val sex : String = "male" //抽象類中的欄位不能用abstract修飾
  }

  //Scala的特質相當於Java中的介面,不過特質中可以有實現的方法
  //Scala中實現特質被成為混入,混入的第一個特質用關鍵字extends,混入更多的用with

  trait Person8{
    val name : String
    val age = 30
    def eat(str:String): Unit ={
      println(str)
    }
  }

  trait Worker{
    val age = 25
    def work: Unit ={
      println("Working....")
    }
  }

  class Student2 extends Worker with Person8{
    val name : String = "zhangsan"
    override val age: Int = 15  //兩個特質中的相同欄位發生衝突時必須重寫該欄位
  }



  /*
  * 2.1.5 Scala Option類詳解
  * Scala中的Option[T]可以是一些[T]或者None物件,它代表一個缺失值
  * 例如hashMapget方法,可能找到則返回值,可能找不到則返回null,這就是一個Option
  * */

  /*
  * 2.1.6 objetc的提取器
  * objetc中提供了apply方法,作用是在建立物件時不直接用new,而是直接用物件加引數,這時呼叫類伴生物件的apply方法
  * */
  var arr1 = Array(1,2,3)
  arr1 = Array.apply(1,2,3)


  //2-18 提取器

  object ExtractorTest{
    def apply(user:String,domain:String)={
      user + "@" + domain
    }

    def unapply(str: String): Option[(String,String)] = {
      val parts = str.split("@")
      if(parts.length == 2){
        Some(parts(0),parts(1))
      }
      else
        {
          None
        }
    }
  }


}

相關文章