[သင္ခန္းစာ ၁၇] Permission မရိွဘဲ အသံုးျပဳခ်င္ပါလား။ method တစ္ခုကို accessလုပ္ဖို႔ limit ထားၾကည့္ရေအာင္။

[သင္ခန္းစာ ၁၇]Permission မရိွဘဲ အသံုးျပဳခ်င္ပါလား။ method တစ္ခုကို accessလုပ္ဖို႔  limit ထားၾကည့္ရေအာင္။

 

ၿပီးခဲ့တဲ့ တစ္ေခါက္တုန္းက access restrictions of the field နဲ႔ပတ္သက္ၿပီး ေလ့လာခဲ့ၾကပါတယ္။

အခုဆက္ၿပီးေတာ့ method ေတြကို access restrictions လုပ္တာနဲ႔ပတ္သက္ၿပီး ပိုၿပီးနားလည္ေအာင္လုပ္ၾကရေအာင္။

"Lecture 16" နဲ႔ပတ္သက္ၿပီးေတာ့ မွတ္မိလိမ့္မယ္လို႔ေမွ်ာ္လင့္ပါတယ္။

method တစ္ခုကို “access restrictions” လုပ္တယ္ဆိုတာဘာလဲ။

စကားလံုးေတြကုိၾကားတဲ့အခါမွာ ခက္တယ္လုိ႔ခံစားရမယ္ဆိုတာနားလည္ပါတယ္။

private” “protected” “public” ေတြကိုေၾကညာျခင္းအားျဖင့္ field ေတြကုိ accessလုပ္တာေတြကုိ ကန္႔သတ္ခ်က္လုပ္ ႏိုင္ပါတယ္။

Lecture 16”  မွာသံုးခဲ့တဲ့ programတစ္ခ်ိဳ႕ကိုျပန္ၾကည့္ၿပီး ေျပာင္းလဲမႈေတြကို confirm လုပ္ၾကည့္ရေအာင္။

public class AnotherClassSample

{

    private String name;                                         // (A)

    // constructer

    public AnotherClassSample()                                  // (B)

    {

        System.out.println("AnotherClassSample Constructor!! ");  // (C)

        name = "foo";                                            // (D)

    }

    // name setting

    public void setName(String newName)

    {

        name = newName;                                         // (E)

    }

    // display the name

    public void printName()

    {

        System.out.println("My name is " + name + ".");          // (F)

    }

}

public class ClassSample

{

    private String msg;                                  // (1)

    public ClassSample()

    {

        System.out.println("Constructor Was Called.");   // (2)

    }

    public void setMessage(String inMsg)

    {

        msg = inMsg;                                     // (3)

    }

    public void printMessage()

    {

        System.out.println("message:" + msg);            // (4)

    }

    public static void main(String[] args)

    {

        ClassSample classSample;                         // (5)

        classSample = new ClassSample();                 // (6)

        classSample.setMessage("Hello Java World! ");     // (7)

        classSample.printMessage();                      // (8)

        AnotherClassSample anotherClass = new AnotherClassSample();

                                                         // (9)

        anotherClass.printName();                        // (10)

        anotherClass.setName("bar");                     // (11)

        anotherClass.printName();                        // (12)

    }

}

The compilation and the practice results are as follows.

lesson17a

AnotherClassSample class ထဲမွာရိွတဲ့ method ေတြကို ClassSample class ထဲကေနေခၚႏိုင္ပါတယ္။ ဘာျဖစ္လို႔လဲ ဆိုေတာ့ program ထဲမွာရိွတဲ့ methodေတြက အားလံုးpublic ေၾကညာထားလို႔ျဖစ္ပါတယ္။

အဲဒါကေတာ့ fieldေတြလိုပဲ method ေတြကုိလည္း public အေနနဲ႔ အသံုးျပဳျခင္းအားျဖင့္ classရဲ႕အျပင္ဘက္ကေန ေခၚႏိုင္တယ္လို႔ေဖာ္ျပတာပဲျဖစ္ပါတယ္။

ၿပီးေတာ့ “private method” ေတြကို ေခၚႏုိင္လားဆိုတာေလ့လာရေအာင္။

AnotherClassSample ကိုျပန္ၾကည့္ရေအာင္။

public class AnotherClassSample

{

    private String name;                                         // (A)

    // constructer

    public AnotherClassSample()                                  // (B)

    {

        System.out.println("AnotherClassSample Constructor!! ");  // (C)

        name = "foo";                                            // (D)

    }

    // name setting

    public void setName(String newName)

    {

        name = newName;                                         // (E)

    }

    // display the name

    // public void printName() ☆☆☆ revise here ☆☆☆

    private void printName()

    {

        System.out.println("My name is " + name + ".");          // (F)

    }

}

အေျဖကိုေလ့လာၾကည့္ရေအာင္။

lesson17b

Field ရဲ႕အေျဖကေတာ့ အတူတူပဲျဖစ္ပါတယ္။ ကိုယ့္ဘာသာကိုယ္ျပင္ၿပီးေလ့လာၾကည့္ႏိုင္မလား။

AnotherClassSample မွာ privateနဲ႔ေၾကညာထားတဲ့ printName method ကို ေခၚၾကည့္ရေအာင္။

PrintMyName method ကို ClassSample class ကေနေခၚႏိုင္ဖို႔အတြက္ public အေနနဲ႔ ျပန္ျပင္ရမယ္ဆိုတာကို မေမ့ပါနဲ႔။

public class AnotherClassSample

{

    private String name;                                         // (A)

    // constructer

    public AnotherClassSample()                                  // (B)

    {

        System.out.println("AnotherClassSample Constructor!! ");  // (C)

        name = "foo";                                            // (D)

    }

    // name setting

    public void setName(String newName)

    {

        name = newName;                                         // (E)

    }

    // display a name

    // public void printName()

    private void printName()

    {

        System.out.println("My name is " + name + ".");          // (F)

    }

    // display the name (possible calling)

    public void printMyName()                                    // ☆ addition ☆

    {

        printName();

    }

}

public class ClassSample

{

    private String msg;                                  // (1)

    public ClassSample()

    {

        System.out.println("Constructor Was Called.");   // (2)

    }

    public void setMessage(String inMsg)

    {

        msg = inMsg;                                     // (3)

    }

    public void printMessage()

    {

        System.out.println("message:" + msg);            // (4)

    }

    public static void main(String[] args)

    {

        ClassSample classSample;                         // (5)

        classSample = new ClassSample();                 // (6)

        classSample.setMessage("Hello Java World! ");     // (7)

        classSample.printMessage();                      // (8)

        AnotherClassSample anotherClass = new AnotherClassSample(); // (9)

        // anotherClass.printName();                     // ☆ revising ☆

        anotherClass.printMyName();                      // (10')

        anotherClass.setName("bar");                     // (11)

        // anotherClass.printName();                     // ☆ revising ☆

        anotherClass.printMyName();                      // (12')

    }

}

အဲဒါက ေကာင္းေကာင္းအလုပ္လုပ္ရဲ႕လား။

အေျဖကေတာ့ အတူတူပဲျဖစ္ပါတယ္။ ရလဒ္အေနနဲ႔ကေတာ့ private and public
အဲဒါဆို private method ကဘာေၾကာင့္လိုအပ္တာလဲ။

ဘာေၾကာင့္လဲဆိုေတာ့ ကိုယ္ရဲ႕program ထဲမွာရိွတဲ့ field ေတြကိုတစ္ျခားသူေတြက change ခြင့္မျပဳခ်င္ဘူးဆိုရင္ သံုးႏိုင္ပါတယ္။ တစ္ျခားနည္းနဲ႔ေျပာရမယ္ဆိုရင္ေတာ့ field ေတြ method ေတြကို private အေနနဲ႔ေၾကညာထားမယ္ဆိုရင္ တစ္ျခားclassေတြက အသံုးမျပဳႏိုင္၊ မေျပာင္းလဲႏိုင္ပါဘူး။

~ summary ~

1. Field ေတြနဲ႔ method ေတြကို access restriction လုပ္တာက အတူတူပဲျဖစ္ပါတယ္။

2. Methods ေတြကို တစ္ျခားclass ေတြကေန အသံုးျပဳတာ၊ ေျပာင္းလဲတာေတြကို ကာကြယ္ဖို႔အေနနဲ႔ private ကိုအသံုးျပဳတာျဖစ္ပါ တယ္။

အခု lecture နဲ႔ ၿပီးခဲ့တဲ့ lecture ကရွင္းျပခဲ့တဲ့အခ်က္ေတြကို မွတ္သားထားရေအာင္။ တစ္ခ်ိန္မွာ အသံုးျပဳရမယ့္ေန႔ေရာက္ လာႏိုင္တဲ့အတြက္ျဖစ္ပါတယ္။

 


Check our Facebook Page!