760 likes | 929 Views
Method. What is a method ? Method คือ คำสั่งที่เกิดจากกลุ่ม ของโค้ด คำสั่งที่รวมกันอยู่ภายใต้คำสั่งเดียว เพื่อให้คำสั่งใหม่ที่ได้นั้น ทำงาน ได้ตามวัตถุประสงค์ ประโยชน์ของ m ethod
E N D
What is a method? Method คือ คำสั่งที่เกิดจากกลุ่มของโค้ดคำสั่งที่รวมกันอยู่ภายใต้คำสั่งเดียว เพื่อให้คำสั่งใหม่ที่ได้นั้นทำงานได้ตามวัตถุประสงค์ ประโยชน์ของ method เมื่อ programmer ได้รับ method มา method นั้นจะเป็น service ให้กับ programmer ในการแก้ไขปัญหา โดยที่ programmer ไม่จำเป็นต้องทราบรายละเอียดภายใน method
กลไกการทำงาน 1. รับค่ามาจากผู้ใช้งาน(Programmer) 2.ประมวลผล 3. ส่งค่ากลับมาในตำแหน่งที่เรียก แนวคิดการทำงานของ method Concept การใช้งาน method คือ ให้เรามอง method เป็น กล่องดำ (Black box) โดยให้เราสนใจเพียงว่า • สิ่งที่เราต้องส่งให้ method คืออะไร? • method ทำอะไร? • ค่าที่ method ส่งกลับมาคืออะไร?
แนวคิดการทำงานของ Method(ต่อ) ค่าที่ส่งให้ method { int x; for(int i=0;i<x;i++){ ………………….. } Method ค่าที่ Method ส่งกลับ
วิเคราะห์ Method readInt() • สิ่งที่เราต้องส่งให้ methodreadInt() คืออะไร? • method readInt()ทำอะไร? • ค่าที่ methodreadInt() ส่งกลับมาคืออะไร? ข้อความที่ต้องการแสดง readInt(…….){ …………………….. …………………….. …………………….. } เลขจำนวนเต็ม จาก keyboard
import acm.program.*; public class Program001 extends ConsoleProgram { public void run() { int x; x=2+readInt(“?”)*3; println(“x = ”+x); } } ตัวอย่างโปรแกรมที่ 1
import acm.program.*; public class Program001 extends ConsoleProgram { public void run() { int x; x=2+readInt(“?”)*3; println(“x = ”+x); } } x = ? ตัวอย่างโปรแกรมที่ 1
import acm.program.*; public class Program001 extends ConsoleProgram { public void run() { int x; x=2+readInt(“?”)*3; println(“x = ”+x); } } x = ? ตัวอย่างโปรแกรมที่ 1 5 ? 5
import acm.program.*; public class Program001 extends ConsoleProgram { public void run() { int x; x=2+readInt(“?”)*3; println(“x = ”+x); } } x = 17 ตัวอย่างโปรแกรมที่ 1 ? 5
import acm.program.*; public class Program001 extends ConsoleProgram { public void run() { int x; x=2+readInt(“?”)*3; println(“x = ”+x); } } x = 17 ตัวอย่างโปรแกรมที่ 1 ? 5 x = 17
ประเภทของ method 1 . Medthod สำเร็จรูป Medthod สำเร็จรูป คือ method ที่มีใน Compiler หรือ Package สำเร็จรูป ที่โปรแกรมดึงมาใช้ (ผ่านการ import) 2. Method ที่ผู้ใช้นิยามขึ้นเอง การสร้าง method ขึ้นมาใช้งานเองเป็นเทคนิคการเขียนโปรแกรมขนาดใหญ่ โดยแบ่งงานขนาดใหญ่ออกเป็นงานย่อยที่มีขนาดเล็กลงแล้วจัดการงานย่อยเหล่านั้นให้อยู่ในรูปของ method
1. Method สำเร็จรูป Method สำเร็จรูปในบางครั้ง Method เหล่านี้จะถูกจัดรวมกันเป็นกลุ่มในรูปแบบของ class ลักษณะการเรียกใช้ Method ที่ถูกเขียนอยู่ใน class class_name. method_name (argument_list)
import acm.program.*; public class Program002 extends ConsoleProgram { public void run() { int a=3,b=4; double c; c=Math.sqrt(a*a+b*b); println(“c = ”+c); } } ตัวอย่างโปรแกรมที่ 2
import acm.program.*; public class Program002 extends ConsoleProgram { public void run() { int a=3,b=4; double c; c=Math.sqrt(a*a+b*b); println(“c = ”+c); } } a=3 b=4 ตัวอย่างโปรแกรมที่ 2
import acm.program.*; public class Program002 extends ConsoleProgram { public void run() { int a=3,b=4; double c; c=Math.sqrt(a*a+b*b); println(“c = ”+c); } } a=3 b=4 c=? ตัวอย่างโปรแกรมที่ 2
import acm.program.*; public class Program002 extends ConsoleProgram { public void run() { int a=3,b=4; double c; c=Math.sqrt(a*a+b*b); println(“c = ”+c); } } a=3 b=4 c=? ตัวอย่างโปรแกรมที่ 2 25
import acm.program.*; public class Program002 extends ConsoleProgram { public void run() { int a=3,b=4; double c; c=Math.sqrt(a*a+b*b); println(“c = ”+c); } } a=3 b=4 c=? ตัวอย่างโปรแกรมที่ 2 5.0
import acm.program.*; public class Program002 extends ConsoleProgram { public void run() { int a=3,b=4; double c; c=Math.sqrt(a*a+b*b); println(“c = ”+c); } } a=3 b=4 c=5.0 ตัวอย่างโปรแกรมที่ 2
import acm.program.*; public class Program002 extends ConsoleProgram { public void run() { int a=3,b=4; double c; c=Math.sqrt(a*a+b*b); println(“c = ”+c); } } a=3 b=4 c=5.0 ตัวอย่างโปรแกรมที่ 2 c = 5.0
2. Method ที่ผู้ใช้นิยามขึ้นเอง Method ที่ผู้ใช้นิยามขึ้นเอง จำแนกได้ 3 แบบ 2.1 Method ที่ไม่มีการรับค่าและส่งค่ากลับ 2.2 Method ที่มีการรับค่าแต่ไม่ส่งค่ากลับ 2.3 Method ที่มีการรับค่าและส่งค่ากลับ
2.1 Method ที่ไม่มีการรับค่าและส่งค่ากลับ รูปแบบทั่วไปของการนิยาม method ที่ไม่มีการส่งค่าและรับค่ากลับ scope void method_name () { statements …………… } * Scope public
import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++) print(“*”); println(); } } ตัวอย่างโปรแกรมที่3
import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++) print(“*”); println(); } } ตัวอย่างโปรแกรมที่3
import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++)print(“*”); println(); } } i=0 ตัวอย่างโปรแกรมที่3
import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++) print(“*”); println(); } } i=0 ตัวอย่างโปรแกรมที่3 *
import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++) print(“*”); println(); } } i=1 ตัวอย่างโปรแกรมที่3 **
import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++) print(“*”); println(); } } i=20 ตัวอย่างโปรแกรมที่3 ********************
import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++) print(“*”); println(); } } ตัวอย่างโปรแกรมที่3 ********************
import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++) print(“*”); println(); } } ตัวอย่างโปรแกรมที่3 ******************** Wisuwat Sunhem
import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++) print(“*”); println(); } } ตัวอย่างโปรแกรมที่3 ******************** Wisuwat Sunhem KMITL
import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++) print(“*”); println(); } } ตัวอย่างโปรแกรมที่3 ******************** Wisuwat Sunhem KMITL ********************
2.2 Method ที่มีการรับค่าแต่ไม่ส่งค่ากลับ รูปแบบทั่วไปของการนิยาม method ที่มีการรับค่าแต่ไม่ส่งค่ากลับ scope void method_name (parameter_list) { statements …………… }
ตัวอย่างParameter_list หากมีการนิยาม method ในลักษณะ public void method_name(int v1,int v2,char v3) { ………………………………….. ………………………………….. } ___________________________________________ หากมีการเรียกใช้ method ในลักษณะดังนี้ method_name(24, -2, ‘A’); ค่าของตัวแปร parameter จะมีค่าดังนี้ v1=24, v2=-2, v3=‘A’
Do you know it? * ** *** **** *****
import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } } ตัวอย่างโปรแกรมที่ 4
import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } } i=1 ตัวอย่างโปรแกรมที่ 4
import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } } i=1 #n=1 #ch=‘*’ ตัวอย่างโปรแกรมที่ 4
i=1 #n=1 #ch=‘*’ $i=0->1 import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } } ตัวอย่างโปรแกรมที่ 4 *
import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } } i=1 #n=1 #ch=‘*’ ตัวอย่างโปรแกรมที่ 4 *
import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } } i=2 ตัวอย่างโปรแกรมที่ 4 *
import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } } i=2 #n=2 #ch=‘*’ ตัวอย่างโปรแกรมที่ 4 *
i=2 #n=2 #ch=‘*’ $i=0->2 import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } } ตัวอย่างโปรแกรมที่ 4 * **
import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } } i=2 #n=2 #ch=‘*’ ตัวอย่างโปรแกรมที่ 4 * **
import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } } i=3 ตัวอย่างโปรแกรมที่ 4 * **
import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } } i=3 #n=3 #ch=‘*’ ตัวอย่างโปรแกรมที่ 4 * **
i=3 #n=3 #ch=‘*’ $i=0->3 import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } } ตัวอย่างโปรแกรมที่ 4 * ** ***
import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } } i=3 #n=3 #ch=‘*’ ตัวอย่างโปรแกรมที่ 4 * ** ***
2.3 Method ที่มีการรับค่าและส่งค่ากลับ รูปแบบทั่วไปของการนิยาม methodที่มีการรับค่าและส่งค่ากลับ scope method_type method_name (parameter_list) { statements …………… return value; } Data_type (int, double, …) * method_type
return value การ return ค่ากลับของ method สามารถอยู่ในลักษณะของ Expression ที่มี data_type -> method_type เช่น method_type เป็น boolean สามารถ return เป็น true,false,(4+4)==(3/2) int สามารถ return เป็น 5, x(int type), 5*2+4-4