kotlin 接口_Kotlin程序| 接口示例

kotlin 接口

接口 (Interface)

  • Kotlin interfaces can contain abstracts methods as well as concrete methods(methods with implementations).

    Kotlin接口可以包含抽象方法和具体方法(带有实现方法的方法)。

  • Kotlin interfaces can have properties but these need to be abstract or to provide accessor implementations.

    Kotlin接口可以具有属性,但是这些属性必须是抽象的或提供访问器实现。

  • Interfaces cannot store state, which makes them different from abstract classes.

    接口无法存储状态,这使它们与抽象类不同。

  • Kotlin methods and properties by default abstract, if no implementation provided.

    默认情况下,如果未提供实现,则Kotlin的方法和属性将抽象。

  • Kotlin's class can implement one or more interfaces.

    Kotlin的类可以实现一个或多个接口。

程序演示Kotlin中接口的示例 (Program demonstrate the example of Interface in Kotlin)

package com.includehelp

// Declare Interface
interface Organization{
    // Abstract Property
    val age:Int

    // Property with accessor implementations
    val name:String
        get() = "Trump"

    // interface method with implementations
    fun printAge(){
        println("Age : $age")
    }

    // abstract method
    fun getSalary(salary:Int)

}

// class implements interface
class Manager:Organization{
    // override interface abstract property
    override val age=32

    // Override interface abstracts method
    override fun getSalary(salary: Int) {
        println("Your Salary : $salary")
    }
}

// Main function, Entry Point of Program
fun main(){
    // Create instance of class
    val organization=Manager()

    // Call function
    organization.printAge()

    // Call function
    organization.getSalary(10000)

    // Access properties
    println("Name : ${organization.name}")
}

Output:

输出:

Age : 32
Your Salary : 10000
Name : Trump


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

kotlin 接口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值