Thuta Learning
ရှာဖွေရန်
IntermediateProgrammingbeginner

Interfaces

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

Interface ဆိုတာ class တစ်ခုက ဘာလုပ်နိုင်ရမလဲဆိုတဲ့ contract ပါ။ Class တစ်ခုဟာ interface တစ်ခုထက်ပိုပြီး implement လုပ်နိုင်တာကြောင့် behavior စနစ်တကျခွဲရေးဖို့အသုံးဝင်ပါတယ်။

kotlin
interface Drivable {
    fun drive()
}

class Car : Drivable {
    override fun drive() {
        println("Driving a car")
    }
}

fun main() {
    val car = Car()
    car.drive()
}
You should see
Driving a car

အနှစ်ချုပ်

Interface က class တွေကို common contract တစ်ခုအောက်မှာ စနစ်တကျရေးနိုင်စေပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Interface implement လုပ်ပြီး required function ကိုမရေးရင် compile error ဖြစ်ပါမယ်။

Practical example — payable

Practical example — payable

kotlin
interface Payable {
    fun pay(amount: Int)
}

class MobileWallet : Payable {
    override fun pay(amount: Int) {
        println("Paid $amount MMK with mobile wallet")
    }
}

fun main() {
    val wallet = MobileWallet()
    wallet.pay(10000)
}

You'll know it worked when: Paid 10000 MMK with mobile wallet

Interfaces | Thuta Learning