Kotlin专题Kotlin编程禅与计算机程序设计艺术

【第5篇】Kotlin的类与继承详解

2019-06-14  本文已影响1人  爱学习的蹭蹭

1、类

class Invoice { ... }
class Empty

2、构造函数

class Person constructor(firstName: String) { ... }
class Person(firstName: String) { ... }
class InitOrderDemo(name: String) {
    //定义一个val 字符串
    val firstProperty = "First property: $name".also(::println)  
    init {
        println("First initializer block that prints ${name}")
    }    
    //定义一个val 字符串
    val secondProperty = "Second property: ${name.length}".also(::println)    
    init {
        println("Second initializer block that prints ${name.length}")
    }
}
class Customer(name: String) {
    val customerKey = name.toUpperCase()
}
class Person(val firstName: String, val lastName: String, var age: Int) { …… }
class Customer public @Inject constructor(name: String) { …… }

更多详情,参见可见性修饰符

3、次构造函数

class Person {
    constructor(parent: Person) {
        parent.children.add(this)
    }
}
class Person(val name: String) {
    constructor(name: String, parent: Person) : this(name) {
        parent.children.add(this)
    }
}
class Constructors {
    init {
        println("Init block")
    }

    constructor(i: Int) {
        println("Constructor")
    }
}
class DontCreateMe private constructor () { ... }

注意:在 JVM 上,如果主构造函数的所有的参数都有默认值,编译器会生成 一个额外的无参构造函数,它将使用默认值。这使得 Kotlin 更易于使用像 Jackson 或者 JPA 这样的通过无参构造函数创建类的实例的库。

class Customer(val customerName: String = "")

4、创建类的实例

val invoice = Invoice()
val customer = Customer("Joe Smith")

注意: Kotlin 并没有 new 关键字
创建嵌套类、内部类与匿名内部类的类实例在嵌套类中有述。

5、类成员

类可以包含:

6、继承

class Example // 从 Any 隐式继承

注意:Any 并不是 java.lang.Object;尤其是,它除了 equals()hashCode()toString() 外没有任何成员。 更多细节请查阅Java互操作性部分。

open class Base(p: Int)
class Derived(p: Int) : Base(p)
class MyView : View {
    constructor(ctx: Context) : super(ctx)
    constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs)
}

7 、覆盖方法

open class Base {
    open fun v() { ... }
    fun nv() { ... }
}
class Derived() : Base() {
    override fun v() { ... }
}
open class AnotherDerived() : Base() {
    final override fun v() { ... }
}

8、覆盖属性

open class Foo {
    open val x: Int get() { …… }
}

class Bar1 : Foo() {
    override val x: Int = ……
}
interface Foo {
    val count: Int
}

class Bar1(override val count: Int) : Foo

class Bar2 : Foo {
    override var count: Int = 0
}

9、派生类初始化顺序

open class Base(val name: String) {

    init { println("Initializing Base") }

    open val size: Int = 
        name.length.also { println("Initializing size in Base: $it") }
}

class Derived(
    name: String,
    val lastName: String
) : Base(name.capitalize().also { println("Argument for Base: $it") }) {

    init { println("Initializing Derived") }

    override val size: Int =
        (super.size + lastName.length).also { println("Initializing size in Derived: $it") }
}

10 、调用超类实现

open class Foo {
    open fun f() { println("Foo.f()") }
    open val x: Int get() = 1
}

class Bar : Foo() {
    override fun f() { 
        super.f()
        println("Bar.f()") 
    }
    
    override val x: Int get() = super.x + 1
}
class Bar : Foo() {
    override fun f() { /* …… */ }
    override val x: Int get() = 0
    
    inner class Baz {
        fun g() {
            super@Bar.f() // 调用 Foo 实现的 f()
            println(super@Bar.x) // 使用 Foo 实现的 x 的 getter
        }
    }
}

11、覆盖规则

open class A {
    open fun f() { print("A") }
    fun a() { print("a") }
}

interface B {
    fun f() { print("B") } // 接口成员默认就是“open”的
    fun b() { print("b") }
}

class C() : A(), B {
    // 编译器要求覆盖 f():
    override fun f() {
        super<A>.f() // 调用 A.f()
        super<B>.f() // 调用 B.f()
  }
}

12、抽象类

open class Base {
    open fun f() {}
}

abstract class Derived : Base() {
    override abstract fun f()
}

13、伴生对象


文章来自koltin英文社区
文章来自koltin中文社区

上一篇 下一篇

猜你喜欢

热点阅读