การเรียก method

ปรับปรุง : 2550-06-12 (แก้ hello12)
ข้อมูลเริ่มต้นจาก thaidev.com
จากเว็บไซต์ thaidev.com (expired) อธิบายเรื่อง OOP ให้เข้าใจได้ง่าย ๆ ผมจึง copy ส่วนหนึ่งของ Code มาให้ดู เพราะแสดงให้เห็นว่าสร้าง Object อย่างไร กำหนดคุณสมบัติ และสั่งให้ทำงานได้อย่างไร ปัจจุบันมีเว็บบอร์ดที่ยอดเยี่ยมมาก [webboard]
ตัวอย่างคลาส ที่เตรียมให้ถูกเรียกใช้
class TAirPlane {
   int color;  // สี เป็น คุณสมบัติอีกแบบ
   static void Fly() { };  // บิน เป็น พฤติกรรม หรือ กริยา ที่ object ทำได้
   static void Land() { };  // ลงจอด เป็น อีกพฤติกรรมหนึ่ง
   TAirPlane() {
     System.out.println("result of constructor");
   }
}

TAirPlane AirPlane1; // สร้าง object ชื่อ AirPlane1 จาก class ชื่อ TAirPlane AirPlane1 = new AirPlane1(); // จองพื้นที่ในหน่วยความจำ จึงจะเริ่มเรียกใช้ได้ AirPlane1.Fly(); // สั่งให้ object AirPlane1 ทำกริยา บิน AirPlane1.color = RED; // เปลี่ยน สี (คุณสมบัติ) ของเครื่องบิน ให้เป็นสีแดง
วิธีเรียกใช้ method แบบต่าง ๆ
    แบบที่ 1 : เรียกใช้ Constructor และใช้พื้นที่ในหน่วยความจำ
    
      class hello1 { public static void main(String args[]) { TAirPlane abc = new TAirPlane(); } }
    แบบที่ 2 : แยกประกาศใช้คลาสและใช้พื้นที่ในหน่วยความจำ
      class hello2 { public static void main(String args[]) { TAirPlane abc; abc = new TAirPlane(); } }
    แบบที่ 3 : ใช้พื้นที่ในหน่วยความจำ และเป็นการเรียกใช้ constructor ซึ่ง class นี้ ต้องอยู่ใน Directory เดียวกัน
      class hello3 { public static void main(String args[]) { new TAirPlane(); } }
    แบบที่ 4 : เรียกใช้ method Fly() แต่จะเรียก constructor มาก่อน ถ้า class นั้นมี constructor
      class hello4 { public static void main(String args[]) { new TAirPlane().Fly(); } }
    แบบที่ 5 : เมื่อสร้างวัตถุขึ้นมา สามารถเรียกใช้ method อื่น โดย constructor ทำงานเฉพาะครั้งแรก
      class hello5 { public static void main(String args[]) { TAirPlane abc = new TAirPlane(); abc.Fly(); abc.Land(); } }
    แบบที่ 6 : แสดงตัวอย่างการเรียก main และต้องส่ง Array of String เข้าไป
      class hello6 { public static void main(String args[]) { TAirPlane abc = new TAirPlane(); String a[] = {}; // new String[0]; abc.main(a); } }
    แบบที่ 7 : เรียกใช้ method ภายในคลาสเดียวกัน
      class hello7 { public static void main(String args[]) { minihello(); } static void minihello() { System.out.println("result of mini hello"); } }
    แบบที่ 8 : เรียกใช้ method แบบอ้างชื่อคลาส ในคลาสเดียวกัน
      class hello8 { public static void main(String args[]) { hello8 x = new hello8(); x.minihello(); } static void minihello() { System.out.println("result of mini hello"); } }
    แบบที่ 9 : เรียกใช้ method แบบไม่กำหนด method เป็น Static พร้อมรับ และคืนค่า
    :: ผลลัพธ์คือ 8
      class hello9 { public static void main(String args[]) { hello9 xx = new hello9(); System.out.println(xx.oho(4)); } int oho(int x) { return (x * 2); } }
    แบบที่ 10 : เรียกใช้ method ภายในคลาสเดียวกัน โดย method สามารถรับ และส่งค่าได้
    :: เรียก method ใน static ตัว method ที่ถูกเรียกต้องเป็น static ด้วย
      class hello10 { public static void main(String args[]) { System.out.println(oho(5)); } static int oho(int x) { x = x * 2; return x; } }
    แบบที่ 11 : ใช้ extends เพื่อการสืบทอด (Inheritance)
    :: Constructor ของ TAirPlane จะไม่ถูกเรียกมาทำงาน
      class hello11 extends TAirPlane { public static void main(String args[]) { Fly(); Land(); } }
    แบบที่ 12 : ใช้ extends เพื่อการสืบทอด (Inheritance) แบบผ่าน constructor
    :: Constructor ของ TAirPlane จะถูกเรียกมาทำงาน
      class hello12 extends TAirPlane { hello12() { Fly(); Land(); } public static void main(String args[]) { new hello12(); } }

แนะนำเว็บใหม่ : ผลการจัดอันดับ
รักลำปาง : thcity.com : korattown.com : topsiam.com : มหาวิทยาลัยโยนก
ศูนย์สอบ : รวมบทความ : ไอทีในชีวิตประจำวัน : ดาวน์โหลด : yourname@thaiall.com
ติดต่อ ทีมงาน ชาวลำปาง มีฝันเพื่อการศึกษา Tel.08-1992-7223