ใน Object-Oriented Programming Methods (เมธอด) คือบล็อกโค้ดที่ทำหน้าที่เฉพาะเจาะจง เช่น เช็คยอดเงินในบัญชีพอซื้อหรือไม่ หรือจัดเรียงชื่อนักเรียนตามตัวอักษร
บทช่วยสอนนี้อธิบายการใช้งานเมธอดใน Java พร้อมตัวอย่าง: สร้างเมธอด ไวยากรณ์ เรียกใช้เมธอด จบแล้วคุณจะใช้ Java methods อย่างมืออาชีพ
บทนำ: Java Methods
Java methods กำหนดบล็อกโค้ดทำหน้าที่เฉพาะ เรียกว่า functions ใน OOP
2 เหตุผลหลักที่นักพัฒนาใช้เมธอด:
- Reuse code: ประกาศครั้งเดียว ใช้หลายครั้ง ไม่ต้องพิมพ์ซ้ำ
- Readable code: โค้ดแยกเป็นบล็อกมีชื่อเฉพาะ อ่านง่ายกว่ารวมใน main program
2 ประเภทเมธอดใน Java:
- Standard library methods: built-in เช่น
println()ในjava.io.PrintStream - User-defined methods: ที่ผู้ใช้สร้างเองในคลาส
ตัวอย่าง built-in println():
class Main {
public static void main(String[] args) {
System.out.println("This is a print statement.");
}
}
ผลลัพธ์: This is a print statement.
User-Defined Methods ใน Java
User-defined methods = เมธอดที่ผู้ใช้สร้างในคลาสตามต้องการ มีบล็อกโค้ดที่เขียนไว้
ไวยากรณ์ประกาศเมธอด:
modifier static returnType methodName (arguments) {
// Code goes here
}
อธิบาย:
- modifier: access type (public, private, ฯลฯ)
- static: optional เรียกเมธอดได้โดยไม่ต้องสร้างออบเจ็กต์
- returnType: ชนิดข้อมูลที่คืน (int, float, String, double, ฯลฯ)
- methodName: ชื่อเมธอด
- arguments: ค่าที่ส่งเข้าเมธอด (0, 1, หรือหลายค่า)
ตัวอย่าง: เมธอดพิมพ์ "The order is ready."
class Main {
public static void orderReady() {
System.out.println("The order is ready.");
}
}
เมธอด orderReady() ไม่รับ arguments ไม่คืนค่า เรียก -> พิมพ์ข้อความ
ตอนนี้ยังไม่ทำงาน เพราะยังไม่ได้ เรียก (call) เมธอด:
import java.util.Scanner;
class Main {
public static void orderReady() {
System.out.println("The order is ready.");
}
public static void main(String[] args) {
orderReady();
}
}
ผลลัพธ์: The order is ready.
ใน main เรียก orderReady() -> โค้ดในเมธอดทำงาน
เมธอดที่รับ Arguments (พารามิเตอร์)
เมธอดรับ arguments ส่งข้อมูลเข้าไป สมมติอยากพิมพ์ "Order #[order number] is ready." ให้ชัดเจนกว่า
class Main {
public static void orderReady(int orderNumber) {
System.out.println("Order #" + orderNumber + " is ready.");
}
public static void main(String[] args) {
orderReady(12);
}
}
ผลลัพธ์: Order #12 is ready.
รับพารามิเตอร์ orderNumber ส่งค่า 12 เข้าไป orderReady() อ่านค่า พิมพ์ "Order #12 is ready."
เมธอดที่คืนค่า (Return Value)
เมธอดคืนค่าให้ main program สมมติสร้างแอปคูณ 2 ตัวเลข คูณในฟังก์ชัน คืนผลลัพธ์
class MultiplyNumbers {
public static int multiplyNumbers(int numberOne, int numberTwo) {
return numberOne * numberTwo;
}
public static void main(String[] args) {
int numberOne = 7;
int numberTwo = 9;
int multiplied = multiplyNumbers(numberOne, numberTwo);
System.out.println(numberOne + " x " + numberTwo + " is: " + multiplied);
}
}
ผลลัพธ์: 7 x 9 is: 63
ขั้นตอน: 1) numberOne=7 2) numberTwo=9 3) เรียก multiplyNumbers(7,9) ผลลัพธ์เก็บใน multiplied 4) พิมพ์ "7 x 9 is: 63"
สรุป
Methods สำคัญใน OOP กำหนดบล็อกโค้ดทำหน้าที่เฉพาะ ใช้ซ้ำได้ หลายครั้ง ทำโค้ดอ่านง่ายและมีประสิทธิภาพ บทช่วยสอนนี้อธิบาย: สร้างเมธอด เรียกใช้ พารามิเตอร์ return statement ตอนนี้คุณใช้ Java methods ได้อย่างมืออาชีพแล้ว!