Android Kotlin Classes and Objects-1

不知道如何測試下列語法可以參考如何使用Android Studio 測試資料?

  1. 多個class與執行順序
  2. init與constructor執行優先序
  3. open繼承與禁止繼承
  4. 不同的寫法,影響override能不能改變值
  5. 繼承與當前Class執行順序
  6. 由inner class去找parent的super
  7. 繼承多個class時,選擇指定的Parent
  8. private protected public internal修飾符差異

  • 1. 多個class與執行順序
  • class InitOrderDemo(name: String) {
        val firstProperty = "First p: $name"
                                .also(::println)
    
        init {
            println("First init test ${name}")
        }
    
        val secondProperty = "Second p: ${name.length}"
                                 .also(::println)
    
        init {
            println("Second init test ${name.length}")
        }
    }
    
    @Test
    fun test() {
        InitOrderDemo("123")
        /*
        First p: 123
        First init test 123
        Second p: 3
        Second init test 3
         */
    }
    

  • 2. init與constructor執行優先序
  • class Constructors {
        init {
            println("Init block")
        }
    
        constructor(i: Int) {
            println("Constructor $i")
        }
    }
    
    @Test
    fun test() {
        Constructors(12)
        //init比constructor早執行,就算init放下面也是如此
        //Init block
        //Constructor 12
    }
    

  • 3. open繼承與禁止繼承
  • open class Shape {
        open fun draw() {
            println("Shape draw")
        }
        fun fill() { /*...*/ }
    }
    
    //因為Circle沒有open,所以不能繼承
    class Circle() : Shape() {
        override fun draw() {
            super.draw()
            println("Circle draw")
        }
    }
    
    open class Rectangle() : Shape() {
        final override fun draw() {
            super.draw()
            println("Rectangle draw")
        }
    }
    
    open class Test2() : Rectangle() {
        //因為Rectangle的draw是final所以禁止override
    }
    

  • 4. 不同的寫法,影響override能不能改變值
  • interface Shape {
        val vertexCount: Int
    }
    
    class Rectangle(
        override val vertexCount: Int = 4
    ) : Shape 
    // Always has 4 vertices
    
    class Polygon : Shape {
        override val vertexCount: Int = 0  
    }
    
    @Test
    fun test() {
        Rectangle(5)
        Polygon()//不能改變vertexCount值
    }
    

  • 5. 繼承與當前Class執行順序
  • open class Base(val name: String) {
    
        init { 
            println("Init a base class") 
        }
    
        open val size: Int = name.length.also { 
            println("Init size: $it") 
        }
    }
    
    class Derived(
        name: String,
        val lastName: String,
    ) : Base(
        name.replaceFirstChar { 
            it.uppercase() 
        }.also { 
            println("Argument for the base class: $it") 
        }
    ) {
        init { 
            println("Init a derived class") 
        }
    
        override val size: Int = 
            (super.size + lastName.length)
                .also { 
            println("Init size: $it") 
        }
    }
    
    @Test
    fun test() {
        Derived("apple", "windows")
        /*
        Argument for the base class: Apple
        Init a base class
        Init size: 5
        Init a derived class
        Init size: 12
         */
    }
    

  • 6. 由inner class去找parent的super
  • open class Rectangle {
        open fun draw() { 
            println("Drawing a rectangle") 
        }
        val borderColor: String get() = "black"
    }
    
    class FilledRectangle: Rectangle() {
        override fun draw() {
            val filler = Filler()
            filler.drawAndFill()
        }
    
        inner class Filler {
            fun fill() { 
                println("Filling") 
            }
            fun drawAndFill() {
                super@FilledRectangle.draw() 
    
                fill()
                println("color " +
                        super@FilledRectangle.borderColor
                        )
     
            }
        }
    }
    
    @Test
    fun test() {
        FilledRectangle().draw()
        /*
        Drawing a rectangle
        Filling
        color black
         */
    }
    

    7. 繼承多個class時,選擇指定的Parent

    open class Rectangle {
        open fun draw() { 
            println("Rectangle draw") 
        }
    }
    
    interface Polygon {
        fun draw() { 
            println("Polygon draw") 
        } // interface members are 'open' by default
    }
    
    class Square() : Rectangle(), Polygon {
        // The compiler requires draw() to be overridden:
        override fun draw() {
            super.draw() // call to Rectangle.draw()
            super.draw() // call to Polygon.draw()
        }
    }
    
    @Test
    fun test() {
        Square().draw()
        /*
        Rectangle draw
        Polygon draw
         */
    }
    

    8. Private protected public internal修飾符差異

    //private:只能在內部看到
    //protected:在內部看得到,繼承後也看得到
    //internal:只能在相同模塊看到
    //public: 任何客戶端都可以見到
    open class Outer {
        private val a = 1
        protected open val b = 2
        internal val c = 3
        val d = 4  // 都沒設定預設 public
    
        protected class Nested {
            public val e: Int = 5
        }
    }
    
    class Subclass : Outer() {
        // a 不可見
        // b、c、d 可見
        // Nested 和 e 可見
    
        override val b = 5   // 繼承父,所以一樣是protected
    }
    
    class Unrelated(val o: Outer) {
        // o.a、o.b 不可見
        // o.c 和 o.d 可見(相同模塊)
        // Outer.Nested 不可見,Nested::e 也不可見
    }
    
    internal open class Example {
    
        class Test1: Example() {//相同模塊所以可以繼承
    
        }
        inner class Test2: Example() {//相同模塊所以可以繼承
    
        }
    }
    
    class Test3: Example() {//不同模塊所以不能繼承
    
    }
    


    上面皆是版本ext.kotlin_version = “1.5.0″測試的


    更多相關可以參考這裡


    相關文章

    Android Kotlin 基本語法1Android Kotlin 基本語法2
    1. var 可變變數
    2. val 不可變更參數
    3. Array 用法
    4. ArrayList 用法
    5. List的filter、sort、map、forEach
    6. when 用法
    7. fun 函式用法
    8. if 表達式
    9. for 表達式
    10. 擴充函數(自定義函式)
    1. 操作符重載
    2. 資料區間用法
    3. map使用方法
    4. 內部靜態變數
    5. 靜態變數
    6. 當資料如果是?型態宣告
    7. 資料使用!!,強制將Int?轉Int
    8. data class用法
    9. 強制轉型
    10. run、with、apply、also用法
    Android Kotlin 基本語法3Android Kotlin 基本語法4
    1. class多建構式
    2. 內部class用法
    3. interface
    4. enum class 用法
    5. sealed class 用法
    6. open 用法
    1. 為基礎類型新增函式
    2. Abstract技巧使用
    3. throw使用方法
    4. 利用let特性處理資料型態?
    5. 快速初始化Array
    6. 快速將兩個資料交換
    7. 自定義待處理的fun
    8. 幫loop設定標籤
    Android Kotlin Classes and Objects-2Android Kotlin 委託屬性
    1. 幫List新增swap功能
    2. 擴充外加函式,幫助程式簡化
    3. T,多類型引入
    4. T搭配in、out使用
    5. 1個對象的object用法
    6. 類型別名
    7. 內聯類
    8. 委託
    1. 變數委託
    2. lazy懶加載用法+String.split
    3. 可觀察屬性observable
    4. 委託內建map
    5. 本地委託屬性,此方法可以減少加載
    6. 委託屬性,唯獨與可讀可寫的方法
    7. 將舊方法棄用

    訂閱Codeilin的旅程,若有最新消息會通知。

    廣告

    發表迴響

    在下方填入你的資料或按右方圖示以社群網站登入:

    WordPress.com 標誌

    您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

    Facebook照片

    您的留言將使用 Facebook 帳號。 登出 /  變更 )

    連結到 %s

    透過 WordPress.com 建置的網站.

    向上 ↑

    %d 位部落客按了讚: