kotlin继承


abstract class Person(open val age:Int){//去掉abstract 默认class以final修饰 不能继承 可以使用open去修饰也可以继承

    abstract fun work()
}
//类想要被继承 必须使用open修饰 或者abstract
//方法想要被复写 也必须open或者abstract修饰 方法复写必须加上override关键字
//属性想要被复写 需要用open修饰
//类的构造函数里的参数 加上val 如果没有字段 则该构造方法里的参数 就可以直接作为字段使用
//如果类定义了该字段 则该参数就是构造方法的局部变量

/*
* 父类需要open才可以被继承
* 分类方法属性需要open才可以被复写
* 接口 接口方法 抽象类默认为open
* 复写父类接口成员需要override关键字
* 
* */
class Programmer( age:Int) :Person(age){
    override val age:Int = age
    override fun work() {
        println("he is coding")
    }
}
class Doctor(override val age:Int):Person(age){
    override fun work() {
        println("he is see patient")
    }
}

class Manage :Driver,Writer{
    override fun write() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun drive() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

}
//构造参数里的变量加上修饰符val 就可以成为属性
class SeniorManager(val driver:Driver,val writer: Writer):Driver,Writer{
    override fun write() {
        writer.write()
    }

    override fun drive() {
        driver.drive()
    }

}

//把Driver能力 付给driver变量 Write能力付给 writer就叫不需要复写接口方法
//by代理关键字 接口代理
class SeniorManager1(val driver:Driver,val writer: Writer):Driver by driver,Writer by writer

class CarDriver :Driver{
    override fun drive() {
        println("正在开车")
    }

}
class PPTWriter:Writer{
    override fun write() {
        println("正在写ppt")
    }

}
interface Driver{
    fun drive()
}
interface Writer{
    fun write()
}

interface B{
    fun x():Int = 0
}

interface C{
    fun x():Int = 1
}

abstract class A{
   open fun x():Int = 5
}
class D(var y:Int = 0):A(),B,C{//集成函数冲突 函数名称参数列表返回值需要一致
    override fun x(): Int {
        println("call x() int in D")
        if(y > 100){
            return y
        }else if(y > 90){
            return super<B>.x()//返回B接口的值
        }else if(y > 80){
            return super<A>.x()
        }else{
            return super<C>.x()//返回C接口的值
        }
        return 0
    }



}
fun main(vararg args:String){
    val person : Person= Programmer(10)
    person.work()
    println(person.age)
    val person2 : Person = Doctor(2)
    person2.work()
    println(person2.age)

    val driver = CarDriver()
    val writer = PPTWriter()
    val mangager = SeniorManager(driver,writer)
    mangager.write()
    mangager.drive()
    val manager1 = SeniorManager1(driver,writer)
    manager1.write()
    manager1.drive()

    println(D(110).x())
    println(D(98).x())
    println(D(81).x())
    println(D(0).x())
}
好的,下面是一个简单的示例: 首先,创建一个继承自Dialog类的Kotlin类,例如CustomDialog,然后实现Dialog中的方法: ```kotlin import android.app.Dialog import android.content.Context import android.os.Bundle import android.view.View import android.view.Window import kotlinx.android.synthetic.main.custom_dialog.* class CustomDialog(context: Context) : Dialog(context), View.OnClickListener { private var listener: OnCustomDialogListener? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) requestWindowFeature(Window.FEATURE_NO_TITLE) setContentView(R.layout.custom_dialog) btn_confirm.setOnClickListener(this) btn_cancel.setOnClickListener(this) } override fun onClick(v: View) { when (v.id) { R.id.btn_confirm -> { listener?.onConfirm() dismiss() } R.id.btn_cancel -> { listener?.onCancel() dismiss() } } } fun setOnCustomDialogListener(listener: OnCustomDialogListener) { this.listener = listener } interface OnCustomDialogListener { fun onConfirm() fun onCancel() } } ``` 在这个类中,我们定义了一个OnCustomDialogListener接口,该接口有两个方法onConfirm和onCancel,分别对应确认和取消按钮的点击事件。我们还在onCreate方法中设置了dialog的布局文件,并为确认和取消按钮设置了点击事件。 接下来,在res/layout目录下创建custom_dialog.xml文件,定义dialog的布局,例如: ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp"> <TextView android:id="@+id/tv_title" android:text="这是一个自定义Dialog" android:textSize="20sp" android:textColor="@android:color/black" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"/> <View android:layout_marginTop="16dp" android:layout_marginBottom="16dp" android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/darker_gray"/> <TextView android:id="@+id/tv_message" android:text="这是一个自定义Dialog的消息" android:textSize="16sp" android:textColor="@android:color/black" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"/> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp"> <Button android:id="@+id/btn_cancel" android:text="取消" android:textColor="@android:color/white" android:background="@drawable/bg_btn_cancel" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content"/> <Button android:id="@+id/btn_confirm" android:text="确认" android:textColor="@android:color/white" android:background="@drawable/bg_btn_confirm" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout> ``` 在这个布局文件中,我们定义了一个包含标题、消息和确认取消按钮的LinearLayout,并为确认和取消按钮设置了样式和点击事件。 最后,在Activity或Fragment中使用CustomDialog: ```kotlin val dialog = CustomDialog(this) dialog.tv_title.text = "提示" dialog.tv_message.text = "这是一个自定义Dialog" dialog.setOnCustomDialogListener(object : CustomDialog.OnCustomDialogListener { override fun onConfirm() { // 点击确认按钮的逻辑 } override fun onCancel() { // 点击取消按钮的逻辑 } }) dialog.show() ``` 在这个示例中,我们首先创建了一个CustomDialog实例,并设置了标题、消息和点击事件。然后,我们使用setOnCustomDialogListener方法设置了OnCustomDialogListener接口,当用户点击确认或取消按钮时,CustomDialog会回调相应的方法。最后,我们调用show方法显示dialog。 至此,一个简单的自定义Dialog就完成了。当然,你可以根据自己的需求修改CustomDialog的样式和逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值