美文网首页
google kotlin 训练营-面向对象

google kotlin 训练营-面向对象

作者: wwq2020 | 来源:发表于2020-07-22 11:24 被阅读0次

    原文

    创建类

    package example.myapp
    
    class Aquarium {
        var width: Int = 20
        var height: Int = 40
        var length: Int = 100
        fun printSize() {
            println("Width: $width cm " +
                    "Length: $length cm " +
                    "Height: $height cm ")
        }
    }
    
    
    fun buildAquarium() {
        val myAquarium = Aquarium()
    }
    
    fun main() {
        buildAquarium()
    }
    

    添加构造函数

    class Aquarium(length: Int = 100, width: Int = 20, height: Int = 40) {
       // Dimensions in cm
       var length: Int = length
       var width: Int = width
       var height: Int = height
    ...
    }
    

    简便写法

    class Aquarium(var length: Int = 100, var width: Int = 20, var height: Int = 40) {
    ...
    }
    

    添加 init 方法

    初始化时候自动执行

    class Aquarium (var length: Int = 100, var width: Int = 20, var height: Int = 40) {
        init {
            println("aquarium initializing")
        }
        init {
            // 1 liter = 1000 cm^3
            println("Volume: ${width * length * height / 1000} l")
        }
    }
    

    二级构造函数

    可以有多个,和 init 一样,但是参数得不一样(重载)

    constructor(numberOfFish: Int) : this() {
        // 2,000 cm^3 per fish + extra room so water doesn't spill
        val tank = numberOfFish * 2000 * 1.1
    }
    

    添加 getter,settr

    var volume: Int
        get() = width * height * length / 1000
        set(value) {
            height = (value * 1000) / (width * length)
        }
    

    使用

    fun buildAquarium() {
        val aquarium6 = Aquarium(numberOfFish = 29)
        aquarium6.printSize()
        aquarium6.volume = 70
        aquarium6.printSize()
    }
    

    可见性

    public 代表class外部可见(一切默认public,包括变量和方法)
    internal 代表模块内可见
    private 代表class内部可见,如果是函数的话源文件内部可见
    protected 和private一样,但是subclasses也可见
    

    成员变量

    如果想让成员变量外部可见但是不可修改
    内部可见也可修改

    var volume: Int
        get() = width * height * length / 1000
        private set(value) {
            height = (value * 1000) / (width * length)
        }
    

    类继承

    类默认不可以派生子类,添加 open 才可以
    成员变量默认子类不可以覆写,添加 open 才可以

    open class Aquarium (open var length: Int = 100, open var width: Int = 20, open var height: Int = 40) {
        open var volume: Int
            get() = width * height * length / 1000
            set(value) {
                height = (value * 1000) / (width * length)
            }
    
    class TowerTank (override var height: Int, var diameter: Int): Aquarium(height = height, width = diameter, length = diameter) {
    
        override var volume: Int
        // ellipse area = π * r1 * r2
        get() = (width/2 * length/2 * height / 1000 * PI).toInt()
        set(value) {
            height = ((value * 1000 / PI) / (width/2 * length/2)).toInt()
        }
    }
    

    抽象类

    abstract class AquariumFish {
        abstract val color: String
    }
    
    

    抽象类的子类
    由于上面的 color 是抽象的,所以子类必须实现

    class Shark: AquariumFish() {
        override val color = "gray"
    }
    
    class Plecostomus: AquariumFish() {
        override val color = "gold"
    }
    

    接口

    interface FishAction  {
        fun eat()
    }
    

    实现接口

    class Shark: AquariumFish(), FishAction {
        override val color = "gray"
        override fun eat() {
            println("hunt and eat fish")
        }
    }
    
    class Plecostomus: AquariumFish(), FishAction {
        override val color = "gold"
        override fun eat() {
            println("eat algae")
        }
    }
    

    单例

    object GoldColor : FishColor {
       override val color = "gold"
    }
    

    class Plecostomus:  FishAction, FishColor by GoldColor {
       override fun eat() {
           println("eat algae")
       }
    }
    

    接口 delegation

    类本身不需要实现相应的接口了,实现来自于 by 后面的对象

    class Plecostomus:  FishAction, FishColor by GoldColor {
       override fun eat() {
           println("eat algae")
       }
    }
    

    数据类

    data class Decoration(val rocks: String) {
    }
    

    使用

    fun makeDecorations() {
        val decoration1 = Decoration("granite")
        println(decoration1)
    
        val decoration2 = Decoration("slate")
        println(decoration2)
    
        val decoration3 = Decoration("slate")
        println(decoration3)
    }
    

    使用 equals 判断是否相等

    println (decoration1.equals(decoration2))
    println (decoration3.equals(decoration2))
    

    解构

    val rock = decoration.rock
    val wood = decoration.wood
    val diver = decoration.diver
    
    val (rock, wood, diver) = decoration
    

    枚举

    enum class Color(val rgb: Int) {
       RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF);
    }
    

    sealed 类

    只允许在声明类的文件中派生子类

    sealed class Seal
    class SeaLion : Seal()
    class Walrus : Seal()
    
    fun matchSeal(seal: Seal): String {
       return when(seal) {
           is Walrus -> "walrus"
           is SeaLion -> "sea lion"
       }
    }
    

    相关文章

      网友评论

          本文标题:google kotlin 训练营-面向对象

          本文链接:https://www.haomeiwen.com/subject/mjezkktx.html