<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ### 1. 單例對象 - scala中沒有靜態方法或靜態變量,可以使用object達到同樣的目的。 ~~~ object Accounts { private var lastNumber = 0 def newUniqueNumber() = { lastNumber += 1; lastNumber } } ~~~ - 對象的構造器在該對象第一次被使用時調用。 - scala對象可以用來實現: > 1. 存放工具函數或常量 > 1. 共享單個不可變實例 > 1. 需要用單個實例協調某個服務 ### 2. 伴生對象 - JAVA中會既有實例方法又有靜態方法的類,Scala中用類和與類同名的“伴生”對象實現。 ~~~ class Account { val id = Account.newUniqueNumber() private var balance = 0.0 def deposit(amount: Double) { balance += amount } def description = "Account " + id + " with balance " + balance } object Account { // 伴生對象 private var lastNumber = 0 private def newUniqueNumber() = { lastNumber += 1; lastNumber } } val acct1 = new Account val acct2 = new Account acct1.deposit(1000) val d1 = acct1.description val d2 = acct2.description ~~~ - 類和它的伴生對象可以相互訪問私有特征。 **總結**:個人理解,scala中引入object就是為了解決沒有靜態變量或靜態方法的問題。 ### 3. 擴展類或特質的對象 - 一個object可以擴展類以及一個或多個特質。 ~~~ abstract class UndoableAction(val description: String) { def undo(): Unit def redo(): Unit } object DoNothingAction extends UndoableAction("Do nothing") { override def undo() {} override def redo() {} } val actions = Map("open" -> DoNothingAction, "save" -> DoNothingAction) actions("open") == actions("save") ~~~ ### 4. apply方法 - 不使用new,而直接使用object(參數1,…,參數N),這時候apply方法會被調用。 ~~~ class Account private (val id: Int, initialBalance: Double) { private var balance = initialBalance def deposit(amount: Double) { balance += amount } def description = "Account " + id + " with balance " + balance } object Account { // The companion object def apply(initialBalance: Double) = new Account(newUniqueNumber(), initialBalance) private var lastNumber = 0 private def newUniqueNumber() = { lastNumber += 1; lastNumber } } val acct = Account(1000.0) val d = acct.description ~~~ ### 5. 應用程序對象 - scala程序都是從對象main方法開始 ~~~ object Hello { def main(args: Array[String]) { println("Hello, World!") } } ~~~ - 保存為Hello.scala文件,執行:scalac Hello.scala編譯文件,執行:scala Hello運行程序。 ### 6. 枚舉 - scala 中沒有枚舉類型,但有枚舉類,Enumeration ~~~ object TrafficLightColor extends Enumeration { val Red, Yellow, Green = Value } TrafficLightColor.Red TrafficLightColor.Red.id object TrafficLightColor extends Enumeration { val Red = Value(0, "Stop") val Yellow = Value(10) // Name "Yellow" val Green = Value("Go") // ID 11 } ~~~ - Value中可以不傳值,可以傳入ID、名稱。 - 枚舉的ID可以通過id方法返回,名稱通過toString方法返回。 【待續】
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看