Saturday, January 25, 2014

OOP පළවන පාඩම



OOP (Object Oriented Programming)

Object Oriented Programming කියන්නේ මොකක්ද කියල අපි මුලින්ම බලමු.අපි c වගේ language  එකක් use කරනකොට මේ Object Oriented Programming හව්වෙන්නේ නෑ.මේකෙදි අපි programme ලියන්නේ object එකක් මත පදනම්වෙලා තමයි.object එකෙක් කියන්නේ මොකක්ද කියල දැම්ම තෙරන්නේ නෑ.ටිකක් පහලට යනකොට තේරේවි.අපි මුලින්ම පොඩි programme එකක් බලල ඉමු .
class DemoBox{
      public static void main(String args[]){
            int length;
            int width;
            int height;
           
            length=12;
            width=5;
            height=3;
           
            int volume;
            volume=length*width*height;
            System.out.println("Volume is:"+volume);

      }
}
මේ programme එකේ වෙන්නේ පෙට්ට්යක ගණත්වය calculate කරන එක තමයි .නමුත් අපිට තව පෙට්ටියක ගණත්වය හොයන්න ඕන නම් programme එක මෙහෙම ලියන්න වෙනවා.
class DemoBox{
      public static void main(String args[]){
            int length;
            int width;
            int height;
           
            length=12;
            width=5;
            height=3;
           
            int volume;
            volume=length*width*height;
            System.out.println("Volume is:"+volume);

            int length1;
            int width1;
            int height1;
           
            length1=120;
            width1=50;
            height1=30;
           
            int volume1;
            volume1=length1*width1*height1;
            System.out.println("Volume is:"+volume1);
      }
}
බලන්න මෙහෙම ලිව්වම code එක ගොඩක් දික්වනවා නේද?මෙන්න මේ වගේ ප්‍රශ්න වලදී තමයි අපිට oop ඕන වෙන්නේ.oop ගැන කතාකරනකොට එන ප්‍රදානම දේ තමයි class එක.අපි දැන් බලමු class එකක් කියන්නේ මොකක්ද කියල.

ජාවා class

ජාවා class එකක් කියන දේ මුලිකවම කිව්වොත් යම්කිසි දෙයක් කරගන්න හදාගන්න ටෙම්ප්ලේට් එකක් තමයි.ඒ ටෙම්ප්ලේට්ටෙම්ප්ලටෙ එකට තියානවා නමක්,attributes ,methods යන ඒවා.එක මෙන්න මේ වගේ රූපයකින් පෙන්නනන්න පුළුවන්.මේකේ පෙන්නලා තියන්නේ Box class එක.
Class Name:-Box
Attributes
height
Width
length
Methods
Volume
Area

හැම class එකකටම නමක් තියෙන්න ඕන.ඒවගේම Attribute කියන්නේ class එකේ අඩංගු මුලික futers ටික.methods කියන්නේ Attributes හා වනත් දේවල් use කරලා කරන්න විවිද වැඩටික.දැන් අපි බලමු උඩින් ලියපු programme එකට අදාල class එක ලියන්නේ කොහොමද කියල.
class Box{//class Name
      int length;//attributs
      int width;
      int height;
     
}
අපි දැන් බලමු හදාගත්තු class එක use කරන හැටි.
class DemoBox{
      public static void main(String args[]){//main method
            Box b1;//line 1.1
            b1=new Box();//line 1.2 
           
            System.out.println(b1);//line 2
           
            b1.length=12;
            b1.width=5;
            b1.height=3;
           
            int volume;
            volume=b1.length*b1.width*b1.height;
            System.out.println("Volume is:"+volume);
      }
}
මුලින්ම කියන්න ඕනේ programme එකක් run වෙන්න නම් main method එකක් තියන class එකක් ඇතුලේ ඒ code ටික තියෙන්න ඕන.නැතිනම් run වෙන්නේ නෑ.line 1.1 දි කරන්නේ Box කියන data type එකෙන් variable එකක් හදන එක.line 1.2 කින් කරන්නේ එම memory location එක refer කරන එක.අපි line 2 වගේ ප්‍රින්ට් කරලා බැලුවොත් පෙන්නේනේ hexadecimal value එකක්.ඒ පෙන්නේනේ memory address එක ඒ object එක refer කරන.ඊටපස්සේ අපිට පුළුවන් object එක use කරලා volume එක calculate කරන්න.දැන් අපිට ඕනේ කියල හිතමු තව Box එකක volume එක හොයන්න අපි කරන්න ඕනේ තව බොෂ් type එකේ object එකක් හදන එක විතරයි.එක පහත පෙන්නලා තියනවා.
Box b2=new Box();
           
b2.length=15;
b2.width=7;
b2.height=4;
Java methods
අපි දැන් බලමු ජාවා method එකක් කියන්නේ මොකක්ද කියල.method එකක් කියන්නේ ,class එකක් කියන්නේ මිනිසෙක් කියල හිතමු attribute කියන්නේ ම්නිසාගේ අත්,ඇස ,පාද,කන් වගේ ඒවාට method කියන්නේ මිනිසාට දුහන්න පලුවන් එහෙනම් run කියන්නේ method එකක්.මං හිතන්නේ method එකක් කියන්නේ මොකක්ද කියල ඔයාලට මුලික වශයෙන් තේරෙන්න ඇති.දැන් අපි බලමු ජාවා method එකක structure එක මොකක්ද කියල.

dataType method_name(parameters){
//method body
      Return ………
}

dataType එකට අපිට මුලික data type යොදාගන්න පලුවන්.ඒ හැර void use කරන්න පලුවන්.

ඉහත අපි ලියපු Box class එකට volume එක calculate කරන්න අපි ජාවා method එකක් ලියමු.

int volume(){
      int volume=length*width*hight;
      return volume;
}
return කරන්නේ method එකේ data type එකේ ජාතියේ value එකක් තමයි.දැන් අපි එක main method එකේදී call කරන විදිය බලමු.
Box b=new Box();
b.length=10;
b.hight=2;
b.width=5;
b.volume();
මං පහත දීල තියන programme එකින් කරන්නේ slender එකක area එකයි volume එකයි හොයන එක.
class Slinder {
      int hight;
      int radius;
      final double pi=3.14;
     
      double area(){//method without Parameters
            double are=(2*pi*radius*hight)+(pi*radius*radius);
            return are;
      }
     
      double volume(){
            double vol=pi*radius*radius*hight;
            return vol;
      }
     
      double area(double pi,int radius,int hight){//method with Parameters
            double are=(2*pi*radius*hight)+(pi*radius*radius);
            return are;
      }
     
      double volume(double pi,int radius,int hight){
            double vol=pi*radius*radius*hight;
            return vol;
      }
     
}

Main class එක
public class demoSylender {

      /**
       * @param args
       */
      public static void main(String[] args) {
            Slinder s=new Slinder();
            s.hight=15;
            s.radius=10;
            s.area();//Calling without parameters
            s.volume();
            s.area(3.14, 50, 20);//calling with Parameters
            s.volume(3.14, 5, 32);

      }

}
මේකේ මං පෙන්නලා තියනවා method එකක් parameters ඇතිව හා නැතිව හදන්නේ කොහොමද කියල.අපිට method එකක අපිට අවශ ප්‍රමාණයක් parameters යොදන්න පලුවන්.නමුත් හොඳට මතක තියාගන්න ඕන method එක call කරනකොට parameters යොදපු අනුව call කරන්න ඕන. parameters තියන area method එක call කරවිට අගයන් ගන්නේ parameters වලට pass වෙච්ච ඒවා.attributes නෙමේ.නමුත් this key word එක යොදාගත්තොත් යොදාගන්නේ attributes වල අගයන්.පහත method එක බලන්න.

double area(double pi,int radius,int hight){//method with Parameters
 double are;
are=(2*this.pi*this.radius*this.hight)+(this.pi*this.radius*this.radius);
            return are;
      }

තවදුරත් get ,set method වලින් class එකට values set කරන විදිය තේරුම් ගන්න පහත දැක්වෙන class එක උදව් වේවි.

class Counter {
      int counter=0;
     
      void inclement(){
            counter++;
      }
      void declementer(){
            counter--;
      }
      void setCounter(int c){
            counter=c;
      }
      int getCounter(){
            return counter;
      }
}
 Constructor Method
Constructor method එක කියන්නේ return type එකක් නැති class එකේ නමටම සමාන method name එකක් ඇති method එකකට.සාමාන්යෙන් අපි class එකක් හදනකොටම automatically constructor method එකක් ජාවා වලින් input කරනවා.එක තමයි අපි object එකක් හදනකොට මෙහෙම call කරන්නේ.(Box b1=new Box())”new Box()”අන්න ඒක තමයි default constructor එක.අපි දැන් බලමු කොහොමද Box class එකට constructor method එක දන්නේ කියල.

class Box{
      int length;
      int width;
      int height;
     
      Box(){//Constructor method
            length=1;
            width=1;
            height=1;
            System.out.println("Box....");     
      }
     
      void printVolume(){
            System.out.println("Volume is:"+getVolume());
      }
     
      void setValues(int l, int w, int h){
            length=l;
            width=w;
            height=h ;
      }
     
      void setLength(int l){
            length=l;
      }
     
      void setWidth(int w){
            width=w;
      }
     
      void setHeight(int h){
            height=h;  
      }
     
      int getVolume(){
            int volume;
            volume=length*width*height;
            return volume;
      }
     
      void printArea(){
            int a=getArea();
            System.out.println("Area of the Box is :"+a);
      }
     
      int getArea(){
            int a;
            a=2*(length*width+length*height+width*height);
            return a;
      }
     
      int getLength(){
            return length;
      }
     
      int getWidth(){
            return width;
      }
     
      int getHeight(){
            return height;
      }
     
      void printValues(){
            System.out.println("Length :"+length);
            System.out.println("Width  :"+width);
            System.out.println("Height :"+height);
      }
}
මේකේ main class එක පහත දැක්වේ.
public class demoBox {

      /**
       * @param args
       */
      public static void main(String[] args) {
            Box s=new Box();
      }

}
මේකේ output එක තමයි මේ
අපි කරලා තියෙන්නේ object එක හදන එක විතරයි.නමුත් Box....**** Constructor **** කියල ප්‍රින්ට් වනවා.එහම වෙන්නේ එතකොට constructor එක call වන නිසා.එහමනම් දැන් ඔයලට තේරෙන්නවා ඇති අපි object එක හදනවා කියල කරන්නේ class එකේ constructure method එකක් call කරන එක කියල.අපිට ඕනෙනම් pharameeters යොදලත් constructor එකක් හදන්න පලුවන්.ඒගැන වැඩිදුරට overloding වලදී කතාකරමු.



No comments: