Kotlin之类与继承

Kotlin 中使用关键字 class 声明类

类声明由类名、类头(指定其类型参数、主构造函数等)以及由花括号包围的类体构成。

class Invoice { /*……*/ }

注意:类体、类头不是必须的
无类体、无类头

class Empty

构造函数

在 Kotlin 中的一个类可以有一个主构造函数以及一个或多个次构造函数

主构造函数
class Person constructor(_firstName: String) { /*……*/ }

//主构造函数没有任何注解或者可见性修饰符,可以省略这个 constructor 关键字
class Person(_firstName: String) { /*……*/ }

主构造函数不能包含任何的代码。初始化的代码可以放到以 init 关键字作为前缀的初始化块(initializer blocks)

注意:未声明属性的构造函数参数不能直接使用,这是个临时的变量,但是可以在init初始化代码块中使用

1、kotlin也提供了在主构造函数直接声明变量属性的方法

class Person(val firstName: String, val lastName: String, var age: Int) { /*……*/ }

2、如果构造函数有注解或可见性修饰符,这个 constructor 关键字是必需的

class Customer public @Inject constructor(name: String) { /*……*/ }

3、私有主构造函数

class DontCreateMe private constructor () { /*……*/ }
次构造函数

1、可以用constructor关键字声明次构造函数:

class Person {
    constructor(parent: Person) {
        //次构造函数
    }
}

2、如果类有主构造函数,次构造需要委托给主构造函数,通过 this 关键字

class Person(val name: String) {
    constructor(name: String, parent: Person) : this(name) {
		//次构造函数
    }
}

3、初始化代码块实际上是主构造函数的一部分,初始化代码块比次构造函数先执行

class Constructors {
    init {
        println("初始化代码块")
    }

    constructor(i: Int) {
        println("次构造函数")
    }
}

创建类的实例

调用类构造函数,Kotlin中没有new

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

继承

在 Kotlin 中所有类都有一个共同的超类 Any

class Example // 从 Any 隐式继承

Any 有三个方法:equals()、 hashCode() 与 toString(),因此 所有类都拥有这三个方法

默认情况下,Kotlin 类是最终(final)的,如果要被继承需要用open 修饰

open class Base // 该类开放继承

继承类

open class Base(p: Int)

class Derived(p: Int) : Base(p)

如果被继承类没有主构造函数,那么每个次构造函数必须使用 super 关键字初始化其基类型,或委托给另一个构造函数做到这一点。
注意,在这种情况下,不同的次构造函数可以调用基类型的不同的构造函数:

class MyView : View {
    constructor(ctx: Context) : super(ctx)

    constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs)
}

覆盖方法

覆盖方法必须加上 override 修饰符

open class Shape {
	//open 修饰的方法才能被覆盖
    open fun draw() { /*……*/ }
    fun fill() { /*……*/ }
}

class Circle() : Shape() {
	//覆盖父类方法 override  修饰
    override fun draw() { /*……*/ }
}

覆盖属性

1、覆盖属性必须加上 override 修饰符

open class Shape {
	//open 修饰的属性才能被覆盖
    open val vertexCount: Int = 0
}

class Rectangle : Shape() {
	//覆盖父类属性 override  修饰
    override val vertexCount = 4
}

2、var 属性覆盖一个 val 属性,但反之则不行

interface Shape {
    val vertexCount: Int
}

class Rectangle(override val vertexCount: Int = 4) : Shape // 总是有 4 个顶点

class Polygon : Shape {
    override var vertexCount: Int = 0  // 以后可以设置为任何数
}

调用超类实现

1、可以使用 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
}

2、内部类中访问外部类的超类,可以通过由外部类名限定的 super 关键字来实现:super@Outer:

class FilledRectangle: Rectangle() {
    override fun draw() { 
        val filler = Filler()
        filler.drawAndFill()
    }

    inner class Filler {
        fun fill() { println("Filling") }
        fun drawAndFill() {
        	 调用 Rectangle 的 draw() 实现
            super@FilledRectangle.draw() 
            fill()
            // 使用 Rectangle 所实现的 borderColor 的 get()
            println("Drawn color ${super@FilledRectangle.borderColor}") 
        }
    }
}

覆盖规则

如果一个类从它的直接超类继承相同成员的多个实现,就必须覆盖这个成员并提供其自己的实现(也许用继承来的其中之一)
如果要调用父类成员,就必须使用由尖括号中超类型名限定的 super,如 “super<Base>”

open class Rectangle {
    open fun draw() { /* …… */ }
}

interface Polygon {
	// 接口成员默认就是“open”的
    fun draw() { /* …… */ } 
}

class Square() : Rectangle(), Polygon {
    // 编译器要求覆盖 draw():
    override fun draw() {
    	// 调用 Rectangle.draw()
        super<Rectangle>.draw() 
        // 调用 Polygon.draw()
        super<Polygon>.draw()
    }
}

抽象类

类以及其中的某些成员可以声明为 abstract。抽象成员在本类中可以不用实现。

我们可以用一个抽象成员覆盖一个非抽象的开放成员

open class Polygon {
    open fun draw() {}
}

abstract class Rectangle : Polygon() {
    abstract override fun draw()
}

进一步抽象类用法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值