April 30, 2012

Class နဲ့ Instance ကို​ကွဲ​အောင်​လုပ်​ပါ

Java ကို​လေ့​လာ​ရာ​တွင် ဦးစွာ​တွေ့​ကြုံ​ရ​မည့် အခက်​အခဲ​များ​ကတော့ Classဆို​တာ​ဘာ​လဲ၊ ပြီး​တော့ Instanceဆို​တာ​ဘာ​လဲ ဟုခေါ်​သော မေးခွန်း​များ​ဖြစ်​ကြ​ပါ​သည်။ ဤ Javaကို​အမြန်​ဆုံး​လေ့​လာ​ခြင်း အခန်း​ဆက် ဘ​လော့​ထဲ​မှာ​တော့ Object Oriented Programming ရဲ့​အမြင်​က မဟုတ်​ပဲ၊ Memoryပေါ်​က Program တစ်​ခုပေါ်​တွင် တည်​ရှိ​သော အမြင်​ပိုင်း​ဆိုင်​ရာ ရှု့​ထောင့်​မှ Classနဲ့ Instance တို့​ရဲ့ ကွဲ​ခြား​ချက်​တွေ​ကို လေ့​လာ​သွား​ပါ​မည်။
Class ဆို​သည်မှာ အဲ့​ဒီ​အစု​အစည်း​ရဲ့ တန်ဖိုး​တွေ​ကို​သတ်​မှတ်ရန်အတွက် အချက်အလက်များ (Field)နဲ့၊ လုပ်ဆောင်​ချက် (Method)များ​ကို စုစည်း​ထား​တဲ့ အစု​အစည်း​တစ်​ခု​လို့​သတ်​မှတ်ထား​၏။ ဤ​ရှင်းလင်း​ချက်​ကို ကြည့်​ရုံဖြင့် Class နဲ့ Instance ကို​ကွဲပြား​ပြီး သိရှိ​နိုင်​မည်​မဟုတ်​ပေ။ ဤသို့ဆိုလျှင် Class နဲ့ Instance ကွာခြားချက်သည် အဘယ်နည်း ဟုမေးစရာရှိပါမည်။ Java နှင့် ပတ်သက်သော စာအုပ်ပေါင်းများစွာတွင် နားလည်လွယ်စေရန် နည်းအမျိုးမျိုးဖြင့် ရှင်းပြထားလေ့ရှိ၏။ ကျွှန်တော်အနှစ်အသက်ဆုံး အဖြေမှာ ကွန်ပျူတာ Memory အပေါ်တွင် ဖြစ်ပေါ်လာပုံ၏ ဒါ​မှ​မဟုတ် ဇာစ်​မြစ် ဟာ​ကွဲပြားခြားနားသည် ဟူသော ရှင်းလင်းချက်ပဲ ဖြစ်ပါသည်။

Class ရဲ့ ဇာစ်​မြစ်​သည် .java ဖိုင်​ကို Compileလုပ်​ပြီး၊ ရ​ရှိ​လာ​တဲ့ .class ဖိုင် ဖြစ်​ပါ​သည်။ Program တစ်​ခု​မှ Class ကို​ဆက်​သွယ် အသုံးပြု​မည် ဆို​လျှင် .class ကို​ဖတ်​ယူ​ပြီး Memoryပေါ်​တွင် Class အဖြစ် နေရာ​ချထား​ပါ​မည်။ တ​ဖန် Instance သည် Memoryပေါ်​တွင် တည်​ရှိ​ပြီး​ဖြစ်​သော Class ကို​အသက်​သွင်း​၍ Memory ပေါ်​မှ​အခြား​သော​နေရာ​တွင် ဖြစ်ပေါ်​လာ​စေ​ပါ​သည်။ မှတ်​ရန်​အချက်​မှာ Classသည် .class ဖိုင်​တစ်​ခု​မှ တစ်​ကြိမ်​တည်း​သာ ဖတ်​ယူ​မွေး​ဖွား​ပြီး၊ Instanceသည် Memory ပေါ်​တွင်​တည်​ရှိ​ပြီး​ဖြစ်​သော Class မှ Memory ရှိ​သ​လောက် အကြိမ်ကြိမ် ကူး​ယူ​မွေး​ဖွား​နိုင်​ခြင်း ဖြစ်​ပေ​သည်။

အမြန်​မှတ်ချက်

Class နှင့် Instanceတို့​သည်၊ တန်ဖိုး​နှင့် လုပ်ဆောင်​ချက်​များ​ကို စုစည်း​ထား​သည့်​အချက်​မှာ တူညီ​ကြ​ပေ​သည်။ သို့ရာတွင် Class သည် .class ဖိုင်​မှ တစ်​ကြိမ်​တည်း​သာ ဖတ်​ယူ​မွေး​ဖွား​ပြီး၊ Instanceသည် Memoryပေါ်​တွင်​တည်​ရှိ​သော Classမှ​တစ်​ဆင့် အကြိမ်ကြိမ် ကူး​ယူ​မွေး​ဖွား​နိုင်​ခြင်း တို့​က​ကွဲပြား​ချက်​တို့ ဖြစ်​ကြ​ပါ​သည်။


သည်လို​ဆို​ရင်Classနဲ့Instanceကို ဘယ်​လို​ခွဲ​ခြား​ပြီး အသုံး​ချ​သင့်​တယ်​ဆို​တာ​ကို လက်တွေ့Codeကို​ရေး​ပြီး လေ့​လာ​ကြည့်​ရအောင်။ အောက်​ပါ န​မှု​နာ​တွေ​ကို​ကြည့်⁠ကြည့်​ကြ​ပါ။ အသီး​သီး class A နဲ့ Bကို​ရေးသား​ထား​ပြီး TestClassမှ​နေ ခေါ်​ယူ​အသုံးပြု​ထား​ပါ​သည်။ class A နဲ့ Bတို့​ရဲ့ ကွဲပြား​ချက်​ကို​လေ့​လာ​ကြည့်​ကြ​ပါ​ဆို့။
class A
public class A {
    static int i;
    static int getTwice() {
        return i*2;
    }
}
class B
public class B {
    int i;
    public int gatTwice() {
        return i * 2;
    }
}

Class AရောBပါ၊ ကိုယ်စီ Member Fieldအနေ​နဲ့ int i ကို​ပိုင်ဆိုင်​ပြီး၊ လုပ်ဆောင်​ချက် int တန်ဖိုး​ကို​ပြန်​ပေး​တဲ့ getTwice methodကို ပိုင်ဆိုင်​ကြ​ပါ​တယ်။ class A ရဲ့ i နဲ့ getTwice() ဟာ Classကို​တိုက်ရိုက်​ဆက်​သွယ်​ပြီး အသုံးပြု​နိုင်​ဖို့​အတွက် အရှေ့​မှာ staticဆို​ပြီ သတ်​မှတ်ချက်​ကို ရေးသား​ရ​ပါ​တယ်။ အဲ့​ဒီ​လို​ရေးသား​ထား​တဲ့ Fields တွေ​နဲ့ method တွေ​ကို static field, static methodဆို​ပြီး ခေါ်ဝေါ်​လေ့​ရှိ​ပါ​တယ်။
တ​ဖန် class B မှာ​ရှိ​တဲ့ field နဲ့ method တွေ​ရဲ့​ရှေ့​မှာ static ဆို​တာ​မ​ရေးသား​ထား​ပါ​ဘူး။ အဲ့​ဒါ​ဟာ class B မှ​တ​ဆင့် ပွား​ယူ​နိုင်​တဲ့ instanceတွေ​မှ​တ​ဆင့် အသုံးပြု​စေ​နိုင်​ဖို့​အတွက် ဖြစ်​ပါ​တယ်။ အဲ့​ဒီ​အတွက် instance field, instance method ဆို​ပြီးခေါ်ဝေါ်​လေ့ ရှိ​ပါ​တယ်။
class Test
public class Test {

    public static void main(String [] args) {
        A.i = 10;
        System.out.println("Twice Of Class A : " + A.getTwice());

        B b = new B();
        b.i = 10;
        System.out.println("Twice of Instance b : " + b.gatTwice());
    }
}
Test Classထဲ​မှာ​တော့ class A နဲ့ B တို့​ရဲ့ အသုံးပြု​ပုံ​တွေ​ကို ဖော်​ပြ​ပေး​နေ​ပါ​တယ်။ A.i ဆို​တဲ့ class field i ရဲ့​တန်ဖိုး​ကို 10ဆို​ပြီး​သတ်​မှတ်​နေ​ပါ​တယ်။ ပြီး​သော​အခါ A.getTwice()ဆို​ပြီး A class ရဲ့ getTwiceလုပ်ဆောင်​ချက်​ကို ခေါ်​ယူ​ပြီး system consoleမှာ​ရေးသား​စေ​ပါ​တယ်။ A class ရဲ့ i နဲ့ getTwice ကို static အနေ​နဲ့ သတ်​မှတ်ထား​တဲ့​အတွက် class A မှ တိုက်ရိုက်​အသုံး​ချ​နိုင်​ခြင်း ဖြစ်​ပါ​တယ်။
တ​ဖန် class B ရဲ့ instance b ကို B b = new B(); ဆို​ပြီး သတ်​မှတ်​ပေး​နေ​ပါ​တယ်။ နောက်​တစ်​ကြောင်း​တွင် b.i = 10ဆို​ပြီး instance b ရဲ့ i ရဲ့​တန်ဖိုး​ဟာ 10 ဖြစ်​ပါ​တယ် လို့​သတ်​မှတ်​နေ​ပြန်​တယ်။ ဆက်​ပြီး instance methodဖြစ်​တဲ့ b.getTwiceကိုခေါ် ပြီး System Consoleမှာ ရေးသား​နေ​ပါ​တယ်။ b instance မှ​နေ​အသုံးပြု​နိုင်​ရန် အတွက် သူ့​ရဲ့ Field နဲ့ methodရဲ့​ရှေ့​မှာ staticဆို​တာ မ​ရေးသား​ထား​ပါ​ဘူး။ အဲ့​ဒီ​လို instance field နဲ့ instance method တွေ​ရှိ​တဲ့​အတွက် B classရဲ့ instance b မှ​တ​ဆင့် အသုံးပြု​နိုင်​ခြင်း​ဖြစ်​ပါ​တယ်။

အမြန်​မှတ်ချက်

Class ရဲ့ static field နဲ့ static methodတွေ​ဟာ memory ပေါ်​မှာ တစ်​ကြိမ်​တည်း​သာ ဖြစ်​တည်​တယ်​ဆို​တဲ့ ထူးခြား​ချက်​ကို အသုံး​ချ​ပြီး Class field နဲ့ လုပ်ဆောင်​ချက် တွေ​ကို​ရေးသား​သင့်​ပါသည်။


တ​နည်း​တွေး​ကြည့်​ကြ​ရအောင်။ class A ကို​ရော instance မ​လုပ်​နိုင်​ဘူး​လား။ လုပ်​နိုင်​ပါ​တယ်။ ဒါ​ပေ​မယ့် class A မှာ static field နဲ့ static method သာ​ပိုင်ဆိုင်​ပြီး insatance fieldတွေ methodတွေ မ​ပါ​တဲ့​အတွက် သူ့​ရဲ့ instance ကို​ပွား​ယူ​လုပ်​အောင် အဓိပ္ပါယ်​မ​ရှိ​နိုင်​ပါ​ဘူး။
ဒါ​ဖြင့် Classလည်း​အဲ​လုပ်​လို့​ရ​တယ်၊ instanceလည်း​အလုပ်​လုပ်​လို့​ရ​တယ်။ ဘာ​လို့ instanceက​လို​အပ်​ရ​တာ​လဲဆို​တာ​ကို စဉ်းစား​ကြည့်​ကြ​ရအောင်။ အောက်​ပါ Personဆို​တဲ့ Classကို​ကြည့်​ပါ။ name နဲ့ ageဆို​တဲ့ instance fieldကို​ပိုင်ဆိုင်​ပြီး introdude() ဆို​တဲ့ instance methodရယ် Person(String name, int age)ဆို​တဲ့ constructor ကို​ပိုင်ဆိုင်​ပါ​တယ်။ Constructorဟာ instance တွေ​ကို ပွား​ယူ​တဲ့​အခါ​မှာ အမြဲ​တမ်းခေါ်​ယူ​ခံ​ရ​မည့် လုပ်ဆောင်​ချက်​တစ်​ခု​ဖြစ်​ပါ​တယ်။ Classမှ​တ​ဆင့် instanceကို လုပ်​ယူ​တဲ့​အခါ​မှာ လုပ်ဆောင်​ရ​မည့် လုပ်ဆောင်​ချက်​ဆို​ရင်​လည်း မ​မှား​နိုင်​ပါ​ဘူး။
သည်Person Constructor ကတော့ String name နဲ့ int age ကို Argumentအနေ​နဲ့ လက်ခံ​ရ​ယူ​ပြီး၊ instance member ဖြစ်​ကြ​တဲ့ name နဲ့ ageမှာ အသီး​သီး အစားထိုး​သတ်​မှတ်​နေ​ပါ​တယ်။ ပြီး​တော့ လုပ်ဆောင်​ချက် introduceထဲ​မှာ​တော့ သူ့​ရဲ့ name နဲ့ ageကို System Consoleမှာ ရေးသား​ပြ​နေ​ပါ​တယ်။
class Person
public class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void introduce() {
        System.out.print("My name is " + name);
        System.out.println(" and I am " + age + "years old.");
    }
}
အောက်​ပါ Test class ထဲ​မှာ​တော့ Person ရဲ့ a instance နဲ့ b instance ကို​လုပ်​ယူ​ပြီး အသီး​သီး​ရဲ့ introduceလုပ်ဆောင်​ချက်​ကို လုပ်ဆောင်​စေ​ပါ​တယ်။

classTest
public class Test {   
    public static void main(String [] args) {

        Person a = new Person("Aung Aung", 30);
        Person b = new Person("Thida", 24);
        a.introduce();
        b.introduce();
    }
}
အထက်​ပါ Test Classကို Run ကြည့်​မည်​ဆို​ပါ​က System Console မှာ အောက်​ပါ​အတိုင်း စာ​ကြောင်း​များ​က ထွက်ပေါ်​လာ​ပါ​လိမ့်​မည်။
My name is Aung Aung and I am 30years old.
My name is Thida and I am 24years old.
အထက်ပါ န​မှု​နာ​ကို​ကြည့်​ခြင်း​အား​ဖြင့် Classတစ်​ခု​မှ မ​တူညီ​သော​တန်ဖိုး​များ​နှင့် မ​တူညီ​သော Instanceများ​ကို အကြိမ်ကြိမ် လုပ်​ယူ​နိုင်​တယ် ဆို​တာ​ကို သိရှိ​နိုင်​ပါ​တယ်။


အမြန်​မှတ်ချက်

Constructorမှ​တ​ဆင့် ပုံစံ​ချ​ယူ​ခြင်း​အား​ဖြင့် Class တစ်​ခု​မှ​နေ Instanceကို အကြိမ်ကြိမ်​ပွား​ယူ​နိုင်​ပါ​သည်။


အလွယ်​ဆုံး​ပြော​ရ​မည်​ဆို​လျှင် Classနဲ့ Instance ဟာ ဒယ်အိုး​နဲ့ ဘိန်း​မုန့်​လို​ပါ​ပဲ။ ဒယ်အိုး​တစ်​လုံး​လို​ပဲ Classဟာ​တစ်​ခု​တည်း​ရှိ​ပြီး၊ ဒယ်အိုး​ထဲ​မှာ​ပါဝင်​တဲ့ အစာ​တွေ​ကို အမျိုး​မျိုး​ပြောင်း​ထည့်​ပြီး၊ ထော​ပါတ်​ဘိန်း​မုန့်​တွေ ရိုး⁠ရိုး​ဘိန်း​မုန့်​တွေ​ကို ဖုတ်​ယူ​နိုင်​သ​လို၊ Classတစ်​ခု​မှ တန်ဖိုး​အမျိုး​မျိုး​ပြောင်း​လည်း​ပြီး Instanceတွေ​ကို အမျိုး​မျိုး​လုပ်​ယူ​နိုင်​ပါ​တယ်။
Class နဲ့ Instanceတို့​ရဲ့ ကွဲ​ခြား​ချက်​ကို​သိရှိ​ပြီး Javaဆန်​တဲ့ Codeများ​ကို​ရေးသား​သွား​ကြ​ပါ​ဆို့။

11 comments:

  1. static の使い方よくわかりました。
    ありがとうございます。

    ReplyDelete
  2. Thanks for your lessons. It is very useful for Java learners and I want to encourage you to write many lessons.

    ReplyDelete
  3. If you are going for most excellent contents like myself, simply go to see this web site every day because
    it gives quality contents, thanks

    ReplyDelete
  4. There is definately a lot to learn about this topic.
    I love all the points you've made.

    ReplyDelete
  5. My spouse and I stumbled over here from a different website
    and thought I should check things out. I like what I see so now i'm following you.
    Look forward to going over your web page for a second time.

    ReplyDelete
  6. Hi, I do think this is a great blog. I stumbledupon it ;) I will revisit yet again since I book-marked it.
    Money and freedom is the greatest way to change, may you be rich and continue to
    guide other people.

    ReplyDelete
  7. It's actually a nice and helpful piece of information.
    I'm glad that you shared this useful information with us.
    Please keep us up to date like this. Thank you for sharing.

    ReplyDelete
  8. I was able to find good info from your articles.

    ReplyDelete
  9. It's a pity you don't have a donate button! I'd
    certainly donate to this fantastic blog! I suppose for now i'll settle for book-marking and adding your RSS feed to my Google account.
    I look forward to brand new updates and will talk about this site with my Facebook group.
    Talk soon!

    ReplyDelete
  10. What's up everyone, it's my first pay a visit at this web page, and piece of writing is really fruitful
    in support of me, keep up posting such content.

    ReplyDelete
  11. Hmm is anyone else having problems with the pictures on this blog
    loading? I'm trying to figure out if its a problem on my end or if it's the blog.
    Any suggestions would be greatly appreciated.

    ReplyDelete