1 子类采用主构造函数

//Person.ktpackage com.example.xhelloimport android.app.Personopen class Person {    var name = ""    var age = 0    fun eat() {        println(name + "is eating. He is + " + age + "years old.")    }}
//Student.ktimport android.app.Personclass Student(val sno: String, val grade: Int) : com.example.xhello.Person() {}
//learnKotlin.kt    val student = Student("a123", 5)    println("student grade is" + student.grade)    println("student son is" + student.sno)    student.eat()

运行结果如图所示:
Android Kotlin继承_第1张图片

2 子类采用主构造函数

//Person.ktpackage com.example.xhelloimport android.app.Personopen class Person (val name: String, val age: Int){}
//Student.ktpackage com.example.xhelloimport android.app.Personclass Student(val sno: String, val grade: Int, name:String, age: Int) :    com.example.xhello.Person(name, age) {    init {        println("Sno is " + sno)        println("grade is " + grade )    }}

Android Kotlin继承_第2张图片
3 子类中既有主构造函数也有次构造函数

//Person.ktpackage com.example.xhelloimport android.app.Personopen class Person (val name: String, val age: Int){}
//Student.ktpackage com.example.xhelloimport android.app.Personclass Student(val sno: String, val grade: Int, name:String, age: Int) :        com.example.xhello.Person(name, age) {    constructor(name: String, age: Int) : this("", 0, name, age){    }    constructor() : this("", 0){    }}

Android Kotlin继承_第3张图片
4 子类中只含有此构造函数

//Person.ktpackage com.example.xhelloimport android.app.Personopen class Person (val name: String, val age: Int){}
//Student.ktpackage com.example.xhelloimport android.app.Personclass Student : com.example.xhello.Person {    constructor(name: String, age: Int) : super(name, age){    }}

Android Kotlin继承_第4张图片

更多相关文章

  1. Android 中保存图片的代码
  2. android之将图片转化为圆形图片
  3. Android 使用decodeFile方法加载手机磁盘中的图片文件
  4. Android:使用SpannableString实现图片替换相应的文字
  5. Android 加载图片并存放在缓存中
  6. android 实现图片的裁剪
  7. Android ImageLoader组件加载图片
  8. Android 两个Activity切换时回调函数调用顺序
  9. 【Android网络编程】获取网络图片,具有缓存功能

随机推荐

  1. Android(安卓)学习之 LayoutInflater
  2. 第一章 andriod studio 安装与环境搭建
  3. Android(安卓)View的绘制过程复习
  4. android 关于程序升级问题
  5. android背景选择器selector用法汇总
  6. Android里面用XML定义菜单资源
  7. Android(安卓)软键盘盖住输入框的问题
  8. csr8811蓝牙芯片porting总结
  9. Android常用控件之悬浮窗(Service实现)
  10. EditText 的常用属性与解释