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

Classes & Objects

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

Class ဆိုတာ object တစ်ခုရဲ့ blueprint ပါ။ Object ထဲမှာ data သိမ်းတဲ့ properties နဲ့ အလုပ်လုပ်တဲ့ functions တွေပါဝင်နိုင်ပါတယ်။ Real project မှာ User, Product, Order, Course စတဲ့ entity တွေကို class နဲ့ဖန်တီးလေ့ရှိပါတယ်။

kotlin
class Customer {
    var name = ""

    fun printName() {
        println("Customer name is $name")
    }
}

fun main() {
    val customer = Customer()
    customer.name = "Kyaw"
    customer.printName()
}
You should see
Customer name is Kyaw

အနှစ်ချုပ်

Class က data နဲ့ behavior ကိုတစ်နေရာတည်းမှာ စနစ်တကျစုစည်းပေးပါတယ်။

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

  • Class ကိုကြေညာထားရုံနဲ့ object မဖြစ်သေးပါ။ Customer() လို့ instance ဖန်တီးမှသုံးလို့ရပါတယ်။

Practical example — product object

Practical example — product object

kotlin
class Product {
    var name = ""
    var price = 0

    fun showInfo() {
        println("$name - $price MMK")
    }
}

fun main() {
    val product = Product()
    product.name = "Keyboard"
    product.price = 45000
    product.showInfo()
}

You'll know it worked when: Keyboard - 45000 MMK

Classes & Objects | Thuta Learning