1 / 76

Method

Method. What is a method ? Method คือ คำสั่งที่เกิดจากกลุ่ม ของโค้ด คำสั่งที่รวมกันอยู่ภายใต้คำสั่งเดียว เพื่อให้คำสั่งใหม่ที่ได้นั้น ทำงาน ได้ตามวัตถุประสงค์ ประโยชน์ของ m ethod

Download Presentation

Method

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Method

  2. What is a method? Method คือ คำสั่งที่เกิดจากกลุ่มของโค้ดคำสั่งที่รวมกันอยู่ภายใต้คำสั่งเดียว เพื่อให้คำสั่งใหม่ที่ได้นั้นทำงานได้ตามวัตถุประสงค์ ประโยชน์ของ method เมื่อ programmer ได้รับ method มา method นั้นจะเป็น service ให้กับ programmer ในการแก้ไขปัญหา โดยที่ programmer ไม่จำเป็นต้องทราบรายละเอียดภายใน method

  3. กลไกการทำงาน 1. รับค่ามาจากผู้ใช้งาน(Programmer) 2.ประมวลผล 3. ส่งค่ากลับมาในตำแหน่งที่เรียก แนวคิดการทำงานของ method Concept การใช้งาน method คือ ให้เรามอง method เป็น กล่องดำ (Black box) โดยให้เราสนใจเพียงว่า • สิ่งที่เราต้องส่งให้ method คืออะไร? • method ทำอะไร? • ค่าที่ method ส่งกลับมาคืออะไร?

  4. แนวคิดการทำงานของ Method(ต่อ) ค่าที่ส่งให้ method { int x; for(int i=0;i<x;i++){ ………………….. } Method ค่าที่ Method ส่งกลับ

  5. วิเคราะห์ Method readInt() • สิ่งที่เราต้องส่งให้ methodreadInt() คืออะไร? • method readInt()ทำอะไร? • ค่าที่ methodreadInt() ส่งกลับมาคืออะไร? ข้อความที่ต้องการแสดง readInt(…….){ …………………….. …………………….. …………………….. } เลขจำนวนเต็ม จาก keyboard

  6. import acm.program.*; public class Program001 extends ConsoleProgram { public void run() { int x; x=2+readInt(“?”)*3; println(“x = ”+x); } } ตัวอย่างโปรแกรมที่ 1

  7. import acm.program.*; public class Program001 extends ConsoleProgram { public void run() { int x; x=2+readInt(“?”)*3; println(“x = ”+x); } } x = ? ตัวอย่างโปรแกรมที่ 1

  8. import acm.program.*; public class Program001 extends ConsoleProgram { public void run() { int x; x=2+readInt(“?”)*3; println(“x = ”+x); } } x = ? ตัวอย่างโปรแกรมที่ 1 5 ? 5

  9. import acm.program.*; public class Program001 extends ConsoleProgram { public void run() { int x; x=2+readInt(“?”)*3; println(“x = ”+x); } } x = 17 ตัวอย่างโปรแกรมที่ 1 ? 5

  10. 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

  11. ประเภทของ method 1 . Medthod สำเร็จรูป Medthod สำเร็จรูป คือ method ที่มีใน Compiler หรือ Package สำเร็จรูป ที่โปรแกรมดึงมาใช้ (ผ่านการ import) 2. Method ที่ผู้ใช้นิยามขึ้นเอง การสร้าง method ขึ้นมาใช้งานเองเป็นเทคนิคการเขียนโปรแกรมขนาดใหญ่ โดยแบ่งงานขนาดใหญ่ออกเป็นงานย่อยที่มีขนาดเล็กลงแล้วจัดการงานย่อยเหล่านั้นให้อยู่ในรูปของ method

  12. 1. Method สำเร็จรูป Method สำเร็จรูปในบางครั้ง Method เหล่านี้จะถูกจัดรวมกันเป็นกลุ่มในรูปแบบของ class ลักษณะการเรียกใช้ Method ที่ถูกเขียนอยู่ใน class class_name. method_name (argument_list)

  13. methodใน class Math

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 2. Method ที่ผู้ใช้นิยามขึ้นเอง Method ที่ผู้ใช้นิยามขึ้นเอง จำแนกได้ 3 แบบ 2.1 Method ที่ไม่มีการรับค่าและส่งค่ากลับ 2.2 Method ที่มีการรับค่าแต่ไม่ส่งค่ากลับ 2.3 Method ที่มีการรับค่าและส่งค่ากลับ

  22. 2.1 Method ที่ไม่มีการรับค่าและส่งค่ากลับ รูปแบบทั่วไปของการนิยาม method ที่ไม่มีการส่งค่าและรับค่ากลับ scope void method_name () { statements …………… } * Scope public

  23. 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

  24. 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

  25. 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

  26. 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 *

  27. 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 **

  28. 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 ********************

  29. 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 ********************

  30. 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

  31. 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

  32. 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 ********************

  33. 2.2 Method ที่มีการรับค่าแต่ไม่ส่งค่ากลับ รูปแบบทั่วไปของการนิยาม method ที่มีการรับค่าแต่ไม่ส่งค่ากลับ scope void method_name (parameter_list) { statements …………… }

  34. ตัวอย่าง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’

  35. Do you know it? * ** *** **** *****

  36. 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

  37. 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

  38. 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

  39. 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 *

  40. 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 *

  41. 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 *

  42. 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 *

  43. 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 * **

  44. 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 * **

  45. 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 * **

  46. 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 * **

  47. 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 * ** ***

  48. 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 * ** ***

  49. 2.3 Method ที่มีการรับค่าและส่งค่ากลับ รูปแบบทั่วไปของการนิยาม methodที่มีการรับค่าและส่งค่ากลับ scope method_type method_name (parameter_list) { statements …………… return value; } Data_type (int, double, …) * method_type

  50. 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

More Related