Kotlin for Android(六)Kotlin类的继承和接口 4000
2017-07-21 22:27
375 查看
Kotlin 继承
Kotlin 中所有类都继承该 Any 类,它是所有类的超类,对于没有超类型声明的类是默认超类:class Example // 从 Any 隐式继承
Any 不是 java.lang.Object;尤其是,它除了 equals()、hashCode()和toString()外没有任何成员。
如果一个类要被继承,可以使用 open 关键字进行修饰。
open class Base(p: Int) // 定义基类 class Derived(p: Int) : Base(p)
构造函数
1.子类有主构造函数
如果子类有主构造函数, 则基类必须在主构造函数中立即初始化。open class Person(var name : String, var age : Int){// 基类 } class Student(name : String, age : Int, var no : String, var score : Int) : Person(name, age) { }
2.子类没有主构造函数
如果子类没有主构造函数,则必须在每一个二级构造函数中用 super 关键字初始化基类,或者在代理另一个构造函数。初始化基类时,可以调用基类的不同构造方法。calss Student : Person { constructor(ctx: Context) : super(ctx) { } constructor(ctx: Context, attrs: AttributeSet) : super(ctx,attrs) { } }
重写
在基类中,使用fun声明函数时,此函数默认为final修饰,不能被子类重写。如果允许子类重写该函数,那么就要手动添加 open 修饰它, 子类重写方法使用 override 关键词:/**用户基类**/ open class Person{ open fun study(){ // 允许子类重写 println("我毕业了") } } /**子类继承 Person 类**/ class Student : Person() { override fun study(){ // 重写方法 println("我在读大学") } }
如果有多个相同的方法(继承或者实现自其他类,如A、B类),则必须要重写该方法,使用super范型去选择性地调用父类的实现。
open class A { open fun f () { print("A") } } interface B { fun f() { print("B") } //接口的成员变量默认是 open 的 } class C() : A() , B{ override fun f() { super<A>.f()//调用 A.f() super<B>.f()//调用 B.f() } }
属性重写
属性重写使用 override 关键字,属性必须具有兼容类型,每一个声明的属性都可以通过初始化程序或者getter方法被重写:open class Foo { open val x: Int get() {return x} } class Bar1 : Foo() { override val x: Int =1 }
你可以用一个var属性重写一个val属性,但是反过来不行。因为val属性本身定义了getter方法,重写为var属性会在衍生类中额外声明一个setter方法
你可以在主构造函数中使用 override 关键字作为属性声明的一部分:
interface Foo { val count: Int } class Bar1(override val count: Int) : Foo class Bar2 : Foo { override var count: Int = 0 }
Kotlin 接口
Kotlin 接口与 Java 8 类似,使用 interface 关键字定义接口,允许方法有默认实现,与抽象类不同的是,接口无法保存状态。它可以有属性但必须声明为抽象或提供访问器实现。:interface MyInterface { fun bar() // 未实现 fun foo() { //已实现 // 可选的方法体 println("foo") } }
接口中的属性
接口中的属性只能是抽象的,不允许初始化值,接口不会保存属性值,实现接口时,必须重写属性:interface MyInterface{ var name:String //name 属性, 抽象的 } class MyImpl:MyInterface{ override var name: String = "runoob" //重载属性 }
函数重写
实现多个接口时,可能会遇到同一方法继承多个实现的问题。例如:interface A { fun foo() { print("A") } // 已实现 fun bar() // 未实现,没有方法体,是抽象的 } interface B { fun foo() { print("B") } // 已实现 fun bar() { print("bar") } // 已实现 } class C : A { override fun bar() { print("bar") } // 重写 } class D : A, B { override fun foo() { super<A>.foo() super<B>.foo() } override fun bar() { super<B>.bar() } }
相关文章推荐
- android Kotlin 继承、派生、接口、构造方式,方法、属性重写
- Kotlin for Android - Hello world
- 简单介绍 Kotlin for Android
- kotlin for android----------MVP模式下(OKHttp和 Retrofit+RxJava)网络请求的两种实现方式
- Android---多线程:继承Thread 类和实现Runnable 接口的区别
- Kotlin for Android
- C\C++ 程序员从零开始学习Android - 个人学习笔记(八) - java基础 - 继承、抽象类、接口、内部类(待续)
- Android知识体系梳理笔记五:Kotlin学习笔记一:类和继承以及Anko(全)的基本使用
- android kotlin类和对象(一)-类和继承
- CaysnPrinter开发包接口说明文档 - CaysnPrinter示例程序Sample1 - 20160623 - For Android
- 概述之Kotlin for Android
- Kotlin for Android(一)在android Studio中配置Kotlin
- Kotlin 1.0 Released: Pragmatic Language for JVM and Android
- Kotlin + Anko for Android
- kotlin-for-android-developers
- Kotlin学习(四)—— 类和对象,继承,覆盖,抽象类,属性和字段,接口,可见性修饰符,扩展
- Kotlin for Android - 实战记录<一>
- kotlin for android----------MVP模式实现登录
- Kotlin Hello World For Android
- Kotlin for Android(五)Kotlin类和对象