在kotlin中如果一个方法可以重写覆盖,就需要用open字段修饰这个方法,同时重写的方法必须使用override修饰,如果没有使用open修饰,则子类不允许命名相同名字的函数,
class NetworkConfig : Base() { override fun draw() { super.draw() } } open class Base { open fun draw() { /*……*/ } fun fill() { /*……*/ } }和重写方法一样,需要用open字段修饰这个属性,同时重写的属性必须使用override修饰,如果没有使用open修饰,则子类不允许命名相同名字的属性
open class Base { open val i: Int = 0 } class NetworkConfig : Base() { override val i = 0 }但是不同的是 你可以用一个 var 属性覆盖一个 val 属性,但反之则不行,因为一个 val 属性本质上声明了一个 get 方法, 而将其覆盖为 var 只是在子类中额外声明一个 set 方法。
属性的覆盖还可以在构造函数里使用
open class Base { open var i: Int = 0 } class NetworkConfig(override var i: Int = 0) : Base() { }#关于基类和子类的执行顺序
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") } } //==================执行结果============================= Argument for Base: Hello Initializing Base Initializing size in Base: 5 Initializing Derived Initializing size in Derived: 10从这里可以看到,基类构造函数执行时,派生类中声明或覆盖的属性都还没有初始化。如果在基类初始化逻辑中(直接或通过另一个覆盖的 open 成员的实现间接)使用了任何一个这种属性,那么都可能导致不正确的行为或运行时故障。设计一个基类时,应该避免在构造函数、属性初始化器以及 init 块中使用 open 成员。
派生类中的代码可以使用 super 关键字调用其超类的函数与属性访问器的实现:
open class Rectangle { open fun draw() { println("Drawing a rectangle") } val borderColor: String get() = "black" } class FilledRectangle : Rectangle() { override fun draw() { super.draw() println("Filling the rectangle") } val fillColor: String get() = super.borderColor }在一个内部类中访问外部类的超类,可以通过由外部类名限定的super关键字来实现:super@Outer:
class FilledRectangle: Rectangle() { fun draw() { /* …… */ } val borderColor: String get() = "black" inner class Filler { fun fill() { /* …… */ } fun drawAndFill() { super@FilledRectangle.draw() // 调用 Rectangle 的 draw() 实现 fill() println("Drawn a filled rectangle with color ${super@FilledRectangle.borderColor}") // 使用 Rectangle 所实现的 borderColor 的 get() } } } class FilledRectangle: Rectangle() { fun draw() { /* …… */ } val borderColor: String get() = "black" //inner 代表内部类 inner class Filler { fun fill() { /* …… */ } fun drawAndFill() { super@FilledRectangle.draw() // 调用 Rectangle 的 draw() 实现 fill() println("Drawn a filled rectangle with color ${super@FilledRectangle.borderColor}") // 使用 Rectangle 所实现的 borderColor 的 get() } } }在Kotlin中,实现继承由下述规则规定:如果一个类从它的直接超类继承相同成员的多个实现, 它必须覆盖这个成员并提供其自己的实现(也许用继承来的其中之一)。 为了表示采用从哪个超类型继承的实现,我们使用由尖括号中超类型名限定的super,如super<Base>:
open class Rectangle { open fun draw() { /* …… */ } } interface Polygon { fun draw() { /* …… */ } // 接口成员默认就是“open”的 } class Square() : Rectangle(), Polygon { // 编译器要求覆盖 draw(): override fun draw() { super<Rectangle>.draw() // 调用 Rectangle.draw() super<Polygon>.draw() // 调用 Polygon.draw() } }类以及其中的某些成员可以声明为abstract。 抽象成员在本类中可以不用实现。 需要注意的是,我们并不需要用open标注一个抽象类或者函数——因为这不言而喻。
我们可以用一个抽象成员覆盖一个非抽象的开放成员
open class Polygon { open fun draw() {} } abstract class Rectangle : Polygon() { abstract override fun draw() }