Android Kotlin-1 基本語法2


Posted by John on 2020-12-06

下面內容有,最後更新於2020/12/06 16:19
1.class多建構式
2.內部class用法
3.enum class 用法
4.sealed class 用法
5.open 用法

  1. class多建構式

     class InfoClass(private var name: String) {
         var number: Int = 1
         init {
             name = "Apple"
         }
         constructor(name: String, number: Int): this(name) {
             this.name = name
             this.number = number
         }
    
         fun getInfo() {
             println("name $name, number $number")
         }
     }
    
     val test = InfoClass("John")
     test.getInfo() //打印 name Apple, number 1
    
     val test2 = InfoClass("John",30)
     test2.getInfo() //打印 name John, number 30
    
  2. 內部class用法

     class MainClass {
         var data = 10
         fun main() {
             val innerClass = InnerClass()
             data += innerClass.addFun()
             println("data = ${data}")
         }
         inner class InnerClass {
             var data = 1
             fun addFun(): Int {
                 return data + 1
             }
         }
     }
    
     val mainClass = MainClass()
     mainClass.main() //打印 data = 12
    
  3. enum class 用法

     enum class EnumClass(val index: Int) {
         Data1(0), Data2(1), Data3(2);
         fun getTag(): String {
             return "$index = $name"
         }
     }
    
     var tag = EnumClass.Data1.getTag()
     println(tag)//打印 0 = Data1
     tag = EnumClass.Data2.getTag()
     println(tag)//打印 1 = Data2
     tag = EnumClass.Data3.getTag()
     println(tag)//打印 2 = Data3
    
  4. sealed class 用法

     sealed class SealedClass {
         data class onFinished(val data: Int): SealedClass()
         object onError: SealedClass()
     }
     fun createSealedClass(index: Int): SealedClass {
         when(index) {
             0 -> {
                 return SealedClass.onFnished(30)
             }
             else -> {
                 return SealedClass.onError
             }
         }
     }
    
     val sealedClass = createSealedClass(0)
     when(sealedClass) {
         SealedClass.onError -> {
    
         }
         //data class 這裡需要使用 is
         is SealedClass.onFnished -> {
             //打印 30
             println("data = ${sealedClass.data}")
         }
     }
    
  5. open 用法

     //open class 才可以給class繼承
     //open fun 才可以繼承方法改寫
     open class BaseClass {
         open fun test1(value: Int) {
             //打印 BaseClass test1 20
             println("BaseClass test1 $value")
         }
    
         fun test2(value: Int) {
             //打印 BaseClass test2 25
             println("BaseClass test2 $value")
         }
     }
     //使用override繼承
     class TextClass: BaseClass() {
         override fun test1(value: Int) {
             super.test1(value)
             //打印 BaseClass test1AA 30
             println("BaseClass test1AA ${value+10}")
         }
     }
    
     val testClass = TextClass()
     testClass.test1(20)
     testClass.test2(25)
     //最後打印
     /*
     BaseClass test1 20
     BaseClass test1AA 30
     BaseClass test2 25
     */
    

#Android #Kotlin #電腦程式 #軟體設計







Related Posts

白話的 Hash Table 簡介

白話的 Hash Table 簡介

Day02 messenger、LINE、Telegram差異

Day02 messenger、LINE、Telegram差異

交作業流程 & eslint

交作業流程 & eslint


Comments