kotlin 接口继承_Kotlin程序| 接口内的继承示例

kotlin 接口继承

接口内的继承 (Inheritance within Interface)

  • An Interface Derive from other interfaces.

    从其他接口派生的接口。

  • The derived interface can override super interface members or declare new functions and properties

    派生的接口可以覆盖超级接口成员或声明新的函数和属性

  • Kotlin class implementing such an interface are only need to define missing implementations

    实现此类接口的Kotlin类仅需要定义缺少的实现

程序演示了Kotlin中接口内继承的示例 (Program demonstrate the example of Inheritance within Interface in Kotlin)

package com.includehelp

// Declare Interface
interface AA{
    // Abstract property
    val count:Int

    // Abstract methods
    fun sayHello()

    // Method with implementation
    fun sayGudMorning(){
        println("Gud Morning, IncludeHelp !!")
    }
}

// Derived interface from another super interface
interface  BB:AA{
    // Abstract methods
    fun sayBye()

    // Override Interface abstract methods, 
    // declare into super interface
    override fun sayHello() {
        println("Hello, IncludeHelp !!")
    }
}

// Declare class implementing interface
class IncludeHelp:BB{
    // Override abstract property
    override val count: Int
        get() = 100

    // Override Interface abstract methods
    override fun sayBye() {
        println("Gud Bye, IncludeHelp !!")
    }
}

// Main function, entry point of program
fun main(){
    // Create instance of class
    val includeHelp = IncludeHelp()

    // Call method
    includeHelp.sayGudMorning()

    // Call method
    includeHelp.sayHello()

    // Call method
    includeHelp.sayBye()

    println("Abstract Property : ${includeHelp.count}")
}

Output:

输出:

Gud Morning, IncludeHelp !!
Hello, IncludeHelp !!
Gud Bye, IncludeHelp !!
Abstract Property : 100


翻译自: https://www.includehelp.com/kotlin/example-of-inheritance-within-interface.aspx

kotlin 接口继承

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例: 首先,创建一个继承自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、付费专栏及课程。

余额充值