วิกิฮาวเป็น "wiki" ซึ่งหมายความว่าบทความหลายๆ บทความของเรานั้นเป็นการร่วมมือกันเขียนของผู้เขียนหลายคน ในการเขียนบทความชิ้นนี้ ผู้คน 15 คน ซึ่งบางคนไม่ขอเปิดเผยตัว ได้ร่วมกันเขียนและปรับปรุงเนื้อหาของบทความอย่างต่อเนื่อง
บทความนี้ถูกเข้าชม 20,906 ครั้ง
ถ้าเพิ่งหัดเขียนโปรแกรมด้วยภาษา Java ก็มีหลายฟังก์ชั่นและคำศัพท์ใหม่ๆ ที่ต้องเรียนรู้ ทั้ง class, method, exception, constructor, variable และอื่นๆ บางทีก็เยอะจนมึน เพราะงั้นให้ค่อยๆ เรียนรู้ไปทีละอย่าง ส่วนบทความวิกิฮาวนี้จะแนะนำวิธีการเรียกใช้ method (call method) ในภาษา java ให้คุณเอง
ขั้นตอน
- 1method เทียบได้กับ "ฟังก์ชั่น" ในภาษาอื่นๆ เช่น ภาษา C เอาไว้ใช้ทำซ้ำโค้ด. method จะประกอบด้วย statement ต่างๆ ซึ่งคุณเรียกใช้ method ได้ผ่าน statement อื่น พอถูกเรียกใช้ (called) แล้ว statement ทั้งหมดที่เป็นส่วนประกอบของ method ก็จะเริ่มทำงาน ตัวอย่าง method ก็เช่น "
public static void methodExample() {}
" ตอนนี้ยังไม่มีโค้ดข้างใน แต่จะเห็น 3 คีย์เวิร์ดที่อยู่หน้าชื่อ method ได้แก่public
,static
และvoid
- 2
public
ที่นำหน้าชื่อ method ใช้บอกว่า method นั้นจะถูกเรียกใช้ได้จากทุกที่ รวมถึง class อื่นๆ ด้วย กระทั่งจาก package (ไฟล์) อื่นๆ ที่แตกต่างกัน ขอแค่คุณ import หรือนำเข้า class ไว้ก็พอ. มีอีก 3 คำที่ใช้แทนpublic
ได้ ก็คือprotected
และprivate
ถ้า method เป็นprotected
แสดงว่าเฉพาะ class นี้และ subclass อื่นๆ (class ต่างๆ ที่ต่อยอดมา) เท่านั้นที่มีสิทธิ์เรียกใช้ method แต่ถ้า method เป็นprivate
แสดงว่า method จะถูกเรียกใช้ได้เฉพาะใน class เท่านั้น ส่วนคีย์เวิร์ดสุดท้ายแทบจะไม่เป็นคำด้วยซ้ำ นี่คือกรณีที่คุณไม่ได้ใส่อะไรในpublic
,protected
หรือprivate
แบบนี้เรียกว่า default หรือ package-private แปลว่าเฉพาะ class ต่างๆ ที่อยู่ใน package เดียวกันเท่านั้น ที่เรียกใช้ method ได้ - 3คีย์เวิร์ดที่ 2
static
ใช้บอกว่า method เป็นของ class นั้น ไม่ได้เป็นของ instance อื่นใน class (object). static method ต้องถูกเรียกใช้ด้วยชื่อ class เช่น "ExampleClass.methodExample()
" แต่ถ้าไม่มีคีย์เวิร์ดstatic
จะเรียกใช้ method ได้ผ่าน object เท่านั้น เช่น ถ้า class ชื่อExampleObject
และมี constructor (สำหรับสร้าง object) คุณก็สร้าง object ใหม่ได้โดยพิมพ์ExampleObject obj = new ExampleObject();
แล้วเรียกใช้ method โดยพิมพ์ "obj.methodExample();
" - 4คำสุดท้ายก่อนชื่อ method คือ
void
.void
แปลว่า method จะไม่แสดงผลอะไร (ไม่มีอะไรเกิดขึ้นเมื่อใช้ method) ถ้าอยากให้ method นั้นแสดงผลอะไรสักอย่าง ก็แค่เปลี่ยนvoid
เป็นชนิดข้อมูล หรือ data type (primitive หรือ reference) ของ object (หรือ primitive) ที่อยากแสดง แล้วพิมพ์return
ตามด้วย object ชนิดนั้น ตรงไหนก็ได้ท้ายโค้ด method - 5ถ้าเรียกใช้ method ที่แสดงผลบางอย่าง ก็ใช้ผลนั้นได้เลย. เช่น ถ้า
someMethod()
แสดงผลเป็นจำนวนเต็ม (integer) ก็กำหนดจำนวนเต็มให้ผลลัพธ์ได้ โดยพิมพ์ "int a = someMethod();
" - 6บาง method ก็ต้องมีพารามิเตอร์. method ที่ต้องใช้พารามิเตอร์ (parameter) ที่เป็นจำนวนเต็ม จะเป็น
someMethod(int a)
เวลาใช้ method แบบนี้ ต้องพิมพ์ชื่อ method ตามด้วยจำนวนเต็มในวงเล็บ เช่นsomeMethod(5)
หรือsomeMethod(n)
โดยn
คือจำนวนเต็ม - 7method มีหลายพารามิเตอร์ได้ แค่คั่นด้วยลูกน้ำ. ถ้า method
someMethod
ต้องใช้ 2 พารามิเตอร์ คือint a
กับObject obj
ก็จะออกมาเป็น "someMethod(int a, Object obj)
" เวลาจะใช้ method ใหม่นี้ ก็ต้องพิมพ์ชื่อ method ตามด้วยจำนวนเต็ม และ Object ในวงเล็บ เช่นsomeMethod(4, thing)
โดยthing
คือObject
โฆษณา
เคล็ดลับ
- เวลาใช้ method ที่แสดงผลได้ ก็เรียกใช้อีก method ได้ แล้วแต่ว่า method นั้นแสดงผลอะไร เช่น ถ้ามี method ชื่อ
getObject()
ที่แสดงผลเป็น object ใน classObject
ก็จะมี non-static method ชื่อtoString
ที่แสดงผลเป็นObject
ในรูปของString
เพราะงั้นถ้าอยากให้String
นั้นของObject
ที่ได้จากgetObject()
อยู่ในบรรทัดเดียว ก็ต้องพิมพ์ว่า "String str = getObject().toString();
"
คำเตือน
- ระวัง abstract class กับ method ถ้า method นั้นเป็น abstract แปลว่ายังใช้งานไม่ได้จนกว่าจะ implement ด้วย class อื่น เพราะ abstract method ไม่มีโค้ดอะไรเลยแต่แรก ส่วน abstract class นั้นถูกใช้เป็น framework รูปแบบหนึ่ง
เกี่ยวกับวิกิฮาวนี้
บทความนี้เป็นประโยชน์กับคุณไหม
⚠️ Disclaimer:
Content from Wiki How ไท language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.
- - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
- - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
- - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
- - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.