วันอังคารที่ 10 พฤศจิกายน พ.ศ. 2558

Polymorphism

Polymorphism


โพลิมอร์ฟิซึม (Polymorphism)
                ประโยชน์ สำคัญของกลไกลโพลิมอร์ฟิซึม  คือการนำกลับมาใช้ใหม่ (reuse) โปรแกรมที่เราออกแบบไว้ ในปัจจุบันสามารถรองรับคลาสใหม่ๆได้ ที่จะถูกเขียนเพิ่มในอานาคต ได้   ทั้งนี้เพราะวัตถุในคลาสใหม่ๆ  จะถูกใช้งานผ่านทางเรพเฟอร์เรนซ์ของคลาสพื้นฐาน 
                

                ส.นอน () สามารถตีความหมายได้หลายแบบ ขึ้นกับวัตถุที่  ส   อ้างถึง
                โปรแกรม แสดง เมธอด นอน ()
                สัตว์ ส ;
                ส = new   สัตว์ ();
                ส.นอน();
                = new   ลิง ();
                ส.นอน();
                = new  ปลา ();
                ส.นอน();



                 สอนโปรแกรมข้างบน  คำสั่ง ส.นอน() แบบเดียวนั้นทำงานที่เมธอดที่แตกต่างกัน  คุณสมบัตินี้เป็นคุณสมบัติของภาษาโปรแกรมเชิงวัตถุที่มีชื่อว่า  โพลิมอร์ฟิซึม (Polymorphism)       ซึ้งโพลิมอร์ฟิซึม จะสนับสนุนการ  การนำกลับมาใช้ใหม่  (eruse)   เช่น  ถ้าเราได้เขียนโปรแกรมที่ให้อาหารสัตว์ โปรแกรมของเราย่อยใช้ได้กับแมว  ปลา และลิง   และถ้ามีคน สร้างคลาสอีกัวน่าขึ้นมาใหม่  โปรแกรมที่เราขียนก็สามารถใช้ได้กับคลาส อีกัวน่าได้เช่นกัน  โดยมีข้อแม้ว่า  แต่คลาสอีกัวน่า จะต้องสืบทอดคลาสสัตว์ หรือ คลาสที่เป็นลูกของควาสสัตว์   

Class  คนเลี้ยงสัตว์
             ให้อาหาร (สัตว์ ) {
                 ส.กิน();
   }
}

 คนเลี้ยงสัตว์  ค =  new คนเลี้ยงสัตว์();
ลิง   ล  =  new  ลิง();
อีกัวน่า  อ  =  new  อีกัวน่า();
ค. ให้อาหาร(ล) ;
ค.ให้อาหาร (อ) ;

            
                                  ภาพ การสืบทอดคลาสสัตว์ที่เป็นคลาสลูก 


ตัวอย่าง ที่1
1     1. คลาสภาพร่างเขียนไว้
Public   class   Shape{
                Public   double getArea(){rrturn 0 ;}
}
นอกจากนี้แล้วภาพร่างยังมีสีตามที่ประกาศไว้ใน อีนัม color
 public   class   Shape{
                private Color color;
                public   Shape(){
                                Color = Color.Red;
                }
                public   void  setColor(Color  c) {
                                Color = c ;
                }
                public Color  getColor(){
                                Return  color;
                }
                public  double getColorArea(){
                                Return 0 ;
                }

     }

1    2. ทดสอบคลาสภาพร่าง
  คลาส TestShape ใช้ทดสอบการทำงานของคลาส  Shape  โดยจะเรียกใช้ทุกเมธอด ในคลาส Shape ในการทดสอบมีขั้นตอนคือ

1.1            สร้างภาพร่าง  พิมพ์สี  และพิมพ์พื้นที่
1.2             สร้างภาพร่างอีกภาพ  เปลี่ยนเป็นสีน้ำเงิน  พิมพ์สี   และพิมพ์พื้นที่
       
public   class  TestShape
        public  static void  main (String[]args){
                        Shape s1 =  new  Shape {
                        System.out.println(s1.getColor());
                        System.out.println(s1.getArea());
                        Shape s2 = new  Shape();

                        S2.setColor(Color.Blue);
                        System.out.println(s2.getColor());
                        System.out.println(s2.getArea());
        }
}

ผลการรันโปรแกรมคือ
Red
0.0
Blue
0.0
                        

ตัวอย่าง 2 TestEmployee.java
package ems.test;

import ems.model.Employee;
import ems.model.Manager;

public class TestEmployee {

                 public static void main(String[] args) {

                 Employee[] empList = new Employee[10];

                  Employee emp1 = new Employee();
                  emp1.setId(1);
    emp1.setName("James");
    emp1.setSalary(15000);
                  emp1.getAddress().setStreet("Rama 3");
    emp1.getAddress().setCity("Bangkok");
    empList[0] = emp1;

                  Employee emp2 = new Employee();
                  emp2.setId(2);
    emp2.setName("Ann");
                   emp2.setSalary(25000);
   emp2.getAddress().setStreet("Silom");
    emp2.getAddress().setCity("Bangkok");
    empList[1] = emp2;

    Manager emp3 = new Manager();
    emp3.setId(3);
    emp3.setName("Peter");
    emp3.setSalary(40000);
    emp3.getAddress().setStreet("Bangrak");
    emp3.getAddress().setCity("Bangkok");
    emp3.setParkingNo("4C-19");
    empList[2] = emp3;
    // because empList will hold address of Employee
    // And Manager is-an Employee. Therefore it's fine

    for (int i = 0; i < empList.length; i++) {
      if (empList[i] != null) {
        System.out.println(empList[i].getDetails());
      } else {
        break;
      }
    }

  } // end of main()

} // end class


ผลการรันโปรแกรมได้ดังนี้
1, James, 15000.0, Rama 3, Bangkok
2, Ann, 25000.0, Silom, Bangkok
3, Peter, 40000.0, Bangrak, Bangkok, [Parking No: 4C-19]

ตัวอย่างที่3
class Overload
{
    void demo (int a)
    {
       System.out.println ("a: " + a);
    }
    void demo (int a, int b)
    {
       System.out.println ("a and b: " + a + "," + b);
    }
    double demo(double a) {
       System.out.println("double a: " + a);
       return a*a;
    }
}
class MethodOverloading
{
    public static void main (String args [])
    {
        Overload Obj = new Overload();
        double result;
        Obj .demo(10);
        Obj .demo(10, 20);
        result = Obj .demo(5.5);
        System.out.println("O/P : " + result);
    }
}


ผลการรันโปรแกรมที่ได้ คือ
a: 10
a and b: 10,20
double a: 5.5
O/P : 30.25          

ตัวอย่างที่ 4
public class BaseClass
{
    public void methodToOverride() //Base class method
    {
         System.out.println ("I'm the method of BaseClass");
    }
}
public class DerivedClass extends BaseClass
{
    public void methodToOverride() //Derived Class method
    {
         System.out.println ("I'm the method of DerivedClass");
    }
}

public class TestMethod
{
     public static void main (String args []) {
        // BaseClass reference and object
        BaseClass obj1 = new BaseClass();
        // BaseClass reference but DerivedClass object
        BaseClass obj2 = new DerivedClass();
        // Calls the method from BaseClass class
        obj1.methodToOverride();
        //Calls the method from DerivedClass class
        obj2.methodToOverride();
     }
}


ผลการรันโปรแกรทที่ได้ คือ
.
I'm the method of BaseClass
I'm the method of DerivedClass


ตัวอย่างที่5
class Vehicle {
    public void move () {
         System.out.println ("Vehicles are used for moving from one place to another ");
    }
}

class Car extends Vehicle {
    public void move () {
      super. move (); // invokes the super class method
      System.out.println ("Car is a good medium of transport ");
    }
}

public class TestCar {
    public static void main (String args []){
        Vehicle b = new Car (); // Vehicle reference but Car object
        b.move (); //Calls the method in Car class
    }
}

ผลการรันโปรแกรทที่ได้ คือ

Vehicles are used for moving from one place to another
Car is a good medium of transport




ไม่มีความคิดเห็น:

แสดงความคิดเห็น

Moodle 19+Font TH

Moodle 19+Font TH ดาวโหลด ดาวโหลด