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

Methods

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

Method သည် reusable code block ဖြစ်ပါတယ်။ တစ်ခါရေးပြီး လိုအပ်တဲ့နေရာတိုင်းခေါ်သုံးနိုင်ပါတယ်။ Code duplicate မဖြစ်အောင်၊ program ကိုသန့်ရှင်းအောင်၊ logic ကိုအပိုင်းခွဲနိုင်အောင် method သုံးပါတယ်။ Method မသုံးဘဲ code အားလုံး main ထဲထည့်ရေးတာက အိမ်တစ်အိမ်လုံးကို အခန်းမခွဲဘဲ တစ်ခန်းတည်းထားသလိုပါ—ရှာရခက်၊ ပြင်ရခက်။

java
public class Main {
  static void greetUser(String name) {
    System.out.println("Hello, " + name + "!");
  }

  static int calculateTotal(int price, int quantity) {
    return price * quantity;
  }

  public static void main(String[] args) {
    greetUser("Aung");
    int total = calculateTotal(20, 3);
    System.out.println("Total: " + total);
  }
}

greetUser method သည် name parameter လက်ခံပြီး greeting ထုတ်ပါတယ်။ calculateTotal method သည် price နဲ့ quantity ကိုလက်ခံပြီး multiplication result ကို return ပြန်ပေးပါတယ်။

You should see
Hello, Aung! Total: 60

လက်တွေ့အသုံးချမှု

Payment total တွက်ခြင်း, user validation, report generation, email sending logic တွေကို method အဖြစ်ခွဲရေးတာက production code မှာအရေးကြီးပါတယ်။

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

  • Return type ရှိတဲ့ method ထဲမှာ return မရေးတာ၊ parameter order မှားပေးတာတွေက common error ပါ။
Methods | Thuta Learning