การเรียก method
Home  Contents KMArticlesMembersSponsorsAbout us

ปรับปรุง : 2556-07-08 (ปรับตัวอย่างใหม่)
OOP :: intro ch1-12 :: keyword & sign :: method calling :: series #1 :: series #2 :: series #3 :: pro_pmy
ขอบซ้ายขอบบน
Class & Method
ความรู้เบื้องต้น
1. คลาส (Class) คือ เสมือนกล่องที่เก็บวัตถุ เก็บได้ทั้ง method, reference และ instance
2. ชื่อคลาสขึ้นต้นด้วยตัวอักษรพิมพ์ใหญ่
3. ชื่อเมธทอดขึ้นต้นด้วยตัวอักษรพิมพ์เล็ก แต่ถ้าในชื่อขึ้นต้นอีกครั้งจะเป็นพิมพ์ใหญ่
    + ต.ย. 1 ชื่อคลาสและเมธทอด Integer a = Integer.parseInt("5");
4. primitive data type คือ ตัวแปรที่ใช้พื้นที่ใน stack memory ไม่ได้ใช้พื้นที่ใน heap memory แบบ instance
    + ต.ย. 2 มี b เป็นชื่อ reference แล้วเก็บค่าใน stack memory คือ int b = 5;
5. instance คือ ผลจากการขอพื้นที่หน่วยความจำเก็บข้อมูล ด้วยคำสั่ง new ซึ่งสามารถถูก reference ได้จากหลายตัวแปร
6. reference คือ ชื่อที่จะไปอ้างอิงข้อมูลใน heap memory เมื่อประกาศ reference ขึ้นมาจะไปอยู่ใน stack memory
    + ต.ย. 3 มี x เป็นชื่อ reference คือ Integer x = new Integer(1);
    + ต.ย. 4 มี y เป็นอีก reference ที่ชี้ไป instance ตัวเดียวกัน Integer y = x;

Term
+ class
+ method
+ instance
+ reference
+ stack memory
+ heap memory
ขอบซ้ายขอบบน
Class sample of TAirplane
คำอธิบาย
ภายในคลาสประกอบด้วย
- 1 instance
- 2 method
- 1 constructor
โดยตัวอย่างนี้เปรียบเทียบว่า คลาสเครื่องบิน มีคุณสมบัติเป็นสี มีพฤติกรรมขึ้นบิน และลงจอด หากมีการเรียกใช้เครื่องบินก็ต้องผ่านการประมวลผลของ constructor ก่อน
ถ้าต้องการ Color
ต้อง import java.awt.*;
class TAirplane {
   Color c;  // สีเครื่องบิน
   static void Fly() { };  // พฤติกรรมขึ้นบิน
   static void Land() { };  // พฤติกรรมลงจอด
   TAirplane() {
     System.out.println("result of constructor");
   }
}
TAirplane Airplane1; // สร้าง reference แบบ TAirplane ชื่อ Airplane1 Airplane1 = new TAirplane(); // จองพื้นที่ในหน่วยความจำ เกิด instance ขึ้นแล้ว Airplane1.Fly(); // สั่งให้เกิดพฤติกรรมขึ้นบิน
import java.awt.*; Airplane1.c = Color.red; // กำหนดสีให้กับเครื่องบินชื่อ Airplane1 // Output = java.awt.Color[r=255,g=0,b=0]
+ เริ่มศึกษาเรื่อง class จาก !thaidev.com แล้วนำมาเขียนตัวอย่างเป็น TAirplane
ขอบซ้ายขอบบน
1. External Method calling
แบบที่ 1.1
- การสร้าง reference ชื่อ a1
- การสร้าง instance สำหรับ a1
- constructor ถูกเรียกใช้ (ถ้ามี)
class E1 {
  public static void main(String args[]) {
    TAirplane  a1 = new TAirplane();
  }
}
แบบที่ 1.2
- สร้าง reference, instance แยกกัน
- หน่วยความจำจะถูกจองตอน new
class E2 {
  public static void main(String args[]) {
    TAirplane  a2;
    a2 = new TAirplane();
  }
}
แบบที่ 1.3
- เรียกใช้ constructor ใน package
- ใน package เดียวกัน จะควานหาให้
- กรณีนี้ไม่มี reference อ้างอิงต่อไม่ได้
class E3 {
  public static void main(String args[]) {
    new TAirplane();
  }
}
แบบที่ 1.4
- เรียก class.method ใน package
- constructor ถูกเรียกก่อน method
- สามารถรับ-ส่งค่าได้ หากมี code เพิ่ม
- กรณีนี้ไม่มี reference อ้างอิงต่อไม่ได้
class E4 {
  public static void main(String args[]) {
    new TAirplane().Fly();
  }
}
แบบที่ 1.5
- เครื่องบินลำที่ 5 มี reference
- เรียกใช้ method ใน instance ได้
- ถ้าสร้าง reference ชี้ a5 จะประหยัด
- E5 จะมีพฤติกรรม 3 อย่าง อะไรบ้าง
- constructor ถูกเรียกครั้งเดียวเท่านั้น
class E5 {
  public static void main(String args[]) {
    TAirplane  a5 = new TAirplane();
    a5.Fly();
    a5.Land();
  }
}
แบบที่ 1.6
- console app. มี default คือ main
- เรียก main ต้องส่งอาร์เรย์ของสตริงค์
class E6 {
  public static void main(String args[]) {
    TAirplane  a6 = new TAirplane();
    String ar[] = {};   // same as new String[0];
    a6.main(ar);
  }
}
ขอบซ้ายขอบบน
2. Internal Method calling
แบบที่ 2.1
- การเรียกใช้ method ใน class
- ทั้ง main และ method เป็น static
class I1 {
  public static void main(String args[]) {
    mymet();
  }
  static void mymet()   {
    System.out.println("result of mymet");
  }
}
แบบที่ 2.2
- เรียก method แบบสร้าง reference
- สามารถไม่กำหนด static ให้ mymet
class I2 {
  public static void main(String args[]) {
    I2 x = new I2();
    x.mymet();
  }
  static void mymet() {
    System.out.println("result of mymet");
  }
}
แบบที่ 2.3
- สร้าง method แบบ return value
- สร้าง local variable ชื่อ y
class I3 {
  public static void main(String args[]) {
    I3 x = new I3();
    System.out.println(x.mymet());
  }
  int mymet() { 
    int y = 5;
    return (y); 
  }
}
แบบที่ 2.4
- method แบบรับค่าเข้า yy
- method แบบคืนค่าเป็น int
class I4 {
  public static void main(String args[]) {
    I4 xx = new I4();
    System.out.println(xx.mymet(4));
  }
  int mymet(int yy) { 
    return (yy * 2); 
  }
}
แบบที่ 2.5
- method แบบรับส่งค่าเป็น int ทั้งคู่
- method นี้ จำเป็นต้องเป็น static
class I5 {
  public static void main(String args[]) {
    System.out.println(mymet(5));
  }
  static int mymet(int x)   {
    x = x * 2;
    return x;
  }
}
ขอบซ้ายขอบบน
3. Inherit Method calling
แบบที่ 3.1
- constructor จะไม่ถูกเรียกมาทำงาน
- กรณีนี้ไม่มี overload หรือ overriding
- กรณีนี้ Fly กับ Land ต้องเป็น static
class In1 extends TAirplane {
  public static void main(String args[]) {
    Fly();
    Land();
  }
}
แบบที่ 3.2
- สร้าง constructor เรียกใช้ method
- เรียก constructor แบบไม่มี reference
class In2 extends TAirplane {
  In2() {
    Fly();
    Land();
  }
  public static void main(String args[]) {
    new In2();
  }
}
แบบที่ 3.3
- เรียก method แบบผ่าน reference จะต้องกำหนดแบบ static
- นำ land มาทำ overload
- นำ fly มาทำ override
class TAirplane {
  static void fly () { System.out.println(5); }
  static void land () { System.out.println(10); }
}
// ===
class myplane extends TAirplane {
  static void land (int xx) { System.out.println(xx); }
  static void fly () { System.out.println(20); }
  public static void main(String args[]) {
    land(); // just inherit
    land(5); // overload
    fly(); // override
  }
}
แบบที่ 3.4
- ถ้าไม่ใช้ void และไม่ใช่ constructor ก็ต้องมีการ return value
- ทำ override land เพื่อกำหนดกิจกรรมใหม่
- การประกาศ iplane ถูกใช้อ้างถึง myplane ในหน่วยความจำใหม่ ทำให้ไม่ต้องประกาศ method เป็น static
class TAirplane {
  void fly () { System.out.println(5); }
  int land (int xxx) { System.out.println(xxx); return xxx;}
}
// ===
class myplane extends TAirplane {
  int land (int xx) { 
    System.out.println(xxx * 2); 
    return(xxx * 2);
  }
  public static void main(String args[]) {
    myplane iplane = new myplane();
    System.out.println(iplane.land(5));
  }
}
แบบที่ 3.5
- ตัวอย่างการสืบทอดของ 3 แฟ้มต่อกัน
- แสดงการทำงานของ 3 คลาสที่สืบทอดกัน
1. a.java
จงเขียน class a มี method ชื่อ int aa(int aaa) แล้วพิมพ์ค่าของ aaa
class a{ int aa(int aaa) { System.out.println(aaa); return aaa;}}

2. b.java
จงเขียน class b มี method ชื่อ void bb(String bbb) 
แล้วพิมพ์ค่าของ bbb โดยคลาส extends a เข้ามา

3. c.java
จงเขียน class c โดยคลาส extends b เข้ามา และมีคำสั่งใน main ดังนี้
c cc = new c();
cc.bb("wow");
System.out.println(cc.aa(5));
"Imagination is more important than knowledge" - Albert Einstein
Home
Thaiabc.com
Thainame.net
Lampang.net
Nation university
PHP
MySQL
Visual basic.NET
TabletPC
Linux
Online quiz
Download
Search engine
Web ranking
Add website
Blog : Education
Blog : ACLA
Blog : Lampang
Facebook.com
Twitter.com
About us
My dream
Site map
Sponsor
http://goo.gl/72BPC