Kotlin for Android(六)Kotlin类的继承和接口
来源:互联网 发布:linux mrtg定制 编辑:程序博客网 时间:2024/05/23 15:27
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) : Fooclass 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() }}
- Kotlin for Android(六)Kotlin类的继承和接口
- Kotlin语法(六)-类和继承
- Kotlin for Android(五)Kotlin类和对象
- Kotlin for Android(七)Kotlin数据类和Gson
- Kotlin学习(七): 类和继承、接口与实现
- Android开发,Kotlin的了解与学习(六)-----类与继承
- Kotlin-类和继承
- Kotlin 类和继承
- Kotlin-类和继承
- Kotlin类、继承、接口实现
- Android Kotlin入门-类和继承
- Kotlin for Android(三)Kotlin数据类型
- Kotlin for Android(九)Kotlin集合
- Kotlin Reference (六) 接口
- [kotlin 系列] (s2_1)类和继承
- Kotlin-2.1-类和继承
- Kotlin类和对象 (二)--- 接口
- Kotlin基础(六)接口、泛型
- js获取一个字符串的某个字符的索引
- 排序算法学习(4)
- linux下c++连接mysql数据库
- tensorflow object detection API 使用记录1
- 浅谈Mysql中的limit关键字
- Kotlin for Android(六)Kotlin类的继承和接口
- Qt信号槽连接函数connect()的简单使用方法
- el 和 jstl 基本入门
- POJ3680-Intervals
- tensorflow练习1:利用神经网络进行分类
- 编程语言入门及进阶、设计模式、面向对象书籍
- Maven Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean Failed to delete
- struts2之ModelDriven的使用问题
- GoogLe图像分类模型 Inception-v2,Inception-v3 论文笔记