SlideShare uma empresa Scribd logo
1 de 12
Baixar para ler offline
Computer Programming
                                               Chapter 5 : Methods




                  Atit Patumvan
  Faculty of Management and Information Sciences
                Naresuna University
2



                                                         โปรแกรมย่อย (Sub Program)


            •         โปรแกรมย่อย มีตําแหน่งในการเข้าถึงชุดคําสั่งเพียงตําแหน่งเดียว

            •         โปรแกรมหลักจะหยุดการทํางาน เมื่อทําการเรียกใช้โปรแกรมย่อย
                      และ

            •         เมื่อโปรแกรมย่อยทํางานเสร็จสิ้นโปรแกรมหลักจะกลับมาทํางาน
                      ต่อในชุดคําสั่งที่อยู่ต่อจาก คําสั่งเรียกใช้โปรแกรมย่อย

            •         โปรแกรมย่อยสามารถแบ่งออกเป็น 2 กลุ่ม

                    •        Procedure เป็นโปรแกรมย่อยที่มีการรับค่าพารามิเตอร์จาก
                             โปรแกรมหลัก

                    •        Function เป็นโปรแกรมย่อยที่มีการส่งค่าให้กับโปรแกรมหลัก

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
3



                                                        การใหลของชุดคําสั่งในโปรแกรม


                                                                Main Program

                                         statements                                          Sub Program


                                                                                      methods
                                         statements


                                                                                     statements
                                      methods call


                                                                                     statements
                                         statements


                                         statements


Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
4



                                                 ตัวอย่างการประยุกต์ใช้โปรแกรมย่อย


                      01: public class PrintStar {                                   *****
                      02:
                      03:     public static void main(String[] args) {               *****
                      04:         System.out.println("*****");
                      05:         System.out.println("*****");                       *****
                      06:         System.out.println("*****");
                      07:         System.out.println("*****");                       *****
                      08:         System.out.println("*****");
                      09:     }                                                      *****
                      10: }

                        :
                      03:               public static void main(String[] args) {
                      04:                   for (int i = 1; i < 6; i++) {
                      05:                       System.out.println("*****");
                      06:                   }
                      07:               }
                        :




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
5



                                                 ตัวอย่างการประยุกต์ใช้โปรแกรมย่อย

                        01: public class PrintStar {
                        02:
                        03:
                        04:
                                public static void main(String[] args) {
                                    for (int i = 0; i < 5; i++) {                     *****
                        05:
                        06:         }
                                        star(5);
                                                                                      *****
                        07:
                        08:
                                }
                                                                                      *****
                        09:
                        10:
                                public static void star(int x) {
                                    for (int i = 0; i < x; i++) {
                                                                                      *****
                        11:
                        12:         }
                                        System.out.print("*");
                                                                                      *****
                        13:         System.out.println();
                        14:     }
                        15: }

                                                                                      *
                          :                                                           **
                        03:                public static void main(String[] args) {
                        04:                    for (int i = 0; i < 5; i++) {          ***
                        05:                        star(i);
                        06:                    }                                      ****
                        07:                }
                          :                                                           *****
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
6



                                                                                     Methods


            •         ในภาษาที่เป็นโครงสร้าง เราสามารถสร้างหน่วยของโปรแกรม
                      (procedure) เพื่อใช้ในการแก้ปัญหาที่ต้องการให้มีการประมวลผล
                      ที่คล้ายๆกัน โดยผู้พัฒนาโปรแกรมไม่ต้องเขียนโปรแกรมซ้ําๆ
                      หลายครั้ง

            •         หน่วยของโปรแกรมที่สามารถส่งค่าออกมาเราจะเรียกว่าฟังก์ชัน
                      (function)

            •         ในภาษาเราจะเรียกหน่วยของโปรแกรมและฟังก์ชัน ด้วยเมธอส
                      (method)




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
7



                                                                        Method Invocation


            •         การเรียกใช้ Method ทําได้หลายแบบ

                    •        Method ที่ไม่ส่งค่ากลับ: จะต้องถูกเรียกจากตําแหน่งประโยค

                    •        Method ที่มีการการส่งค่ากลับ: จะต้องถูกเรียกจาก term
                             ภายใน expression

                    •        โครงสร้างการเรียกใช้เป็นดังนี้                          <method_name> ([argument list])

                                    01: public class MethodInvocation {
                                    02:
                                    03:     public static void main(String[] args) {
                                    04:         method();
                                    05:     }
                                    06:
                                    07:     public static void method() {
                                    08:         System.out.println("sub program is called");
                                    09:     }
                                    10: }

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
8



                                                  การเรียก Method ที่มีการส่งค่ากลับ


                   01: public class FunctionInvocation {
                   02:
                   03:     public static void main(String[] args) {
                   04:         int x = 7, y = 5;
                   05:         int z = add(x,y);
                   06:         System.out.println(z);
                   07:     }
                   08:
                   09:     public static int add(int a, int b) {
                   10:         int c = a + b;
                   11:         return c;
                   12:     }
                   13: }



                                                                                     send value (parameter)

                                                           caller method
                                                                                                              calee method

                                                                                          return value




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
9



                                                                     Method Overloading


            •         การที่เมธอสมีชื่อเหมือนกันได้มากกว่าหนึ่งเมธอส
                      01: public class MethoadOverload {
                      02:
                      03:     public static void main(String[] args) {
                      04:         int x = 7, y = 5;
                      05:         double z = 5.0;
                      06:
                      07:         System.out.println(x / y);
                      08:
                      09:         System.out.println(div(x, y));
                      10:
                      11:         System.out.println(div(x, z));
                      12:     }
                      13:
                      14:     public static double div(int a, int b) {
                      15:         double c = a / (double) b;
                      16:         return c;
                      17:     }
                      18:
                      19:     public static double div(int a, double b) {
                      20:         double c = a / b;
                      21:         return c;
                      22:     }
                      23: }
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
10



                                            Parameter Passing: Pass by Value


                  01: public class PassByValue {
                  02:
                  03:     public static void main(String[] args) {
                  04:         String name = "alice";
                  05:         method(name);
                  06:         System.out.println(name);
                  07:     }
                  08:
                  09:     public static void method(String arg) {
                  10:         arg = "bob" ;
                  11:     }
                  12: }




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
11



                                   Parameter Passing: Pass by Reference

                   01:      public class PassByReference {
                   02:
                   03:               public static void main(String[] args) {
                   04:                   Person p = new Person();
                   05:                   p.setName("alice");
                   06:                   method(p);
                   07:                   System.out.println(p.getName());
                   08:               }
                   09:
                   10:               public static void method(Person arg) {
                   11:                   arg.setName("bob");
                   12:               }
                   13:      }
                   14:
                   15:      class Person {
                   16:
                   17:               private String name;
                   18:
                   19:               public String getName() {
                   20:                   return name;
                   21:               }
                   22:
                   23:               public void setName(String name) {
                   24:                   this.name = name;
                   25:               }
                   26:      }
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
12



                                                                         Scope of Visibility


                            static



                             instance


                                     method
                                                                        block            block




                                   method
                                                                        block




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

Mais conteúdo relacionado

Destaque

การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6Atit Patumvan
 
Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Atit Patumvan
 
Total Quality Management-A Road Map to Corporate Excellence
Total Quality Management-A Road Map to Corporate ExcellenceTotal Quality Management-A Road Map to Corporate Excellence
Total Quality Management-A Road Map to Corporate Excellenceharikrishnanjl
 
TQM (Total Quality Management)
TQM (Total Quality Management)TQM (Total Quality Management)
TQM (Total Quality Management)amarchand
 
total quality management (tqm)
total quality management (tqm)total quality management (tqm)
total quality management (tqm)Dr. Sunil Kumar
 
Tools and techniques used in tqm ppt
Tools and techniques used in tqm pptTools and techniques used in tqm ppt
Tools and techniques used in tqm pptabhandary
 
Total Quality Management (TQM)
Total Quality Management (TQM)Total Quality Management (TQM)
Total Quality Management (TQM)Mudassar Salman
 
Total quality management tools and techniques
Total quality management tools and techniquesTotal quality management tools and techniques
Total quality management tools and techniquesbhushan8233
 

Destaque (17)

การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2
 
การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4
 
การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5
 
การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6
 
Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)
 
05 classification 1 decision tree and rule based classification
05 classification 1 decision tree and rule based classification05 classification 1 decision tree and rule based classification
05 classification 1 decision tree and rule based classification
 
Total Quality Management-A Road Map to Corporate Excellence
Total Quality Management-A Road Map to Corporate ExcellenceTotal Quality Management-A Road Map to Corporate Excellence
Total Quality Management-A Road Map to Corporate Excellence
 
TQM (Total Quality Management)
TQM (Total Quality Management)TQM (Total Quality Management)
TQM (Total Quality Management)
 
Tqm
TqmTqm
Tqm
 
total quality management (tqm)
total quality management (tqm)total quality management (tqm)
total quality management (tqm)
 
หลักการเขียนผังงาน(Flow chart)
หลักการเขียนผังงาน(Flow chart)หลักการเขียนผังงาน(Flow chart)
หลักการเขียนผังงาน(Flow chart)
 
Tools and techniques used in tqm ppt
Tools and techniques used in tqm pptTools and techniques used in tqm ppt
Tools and techniques used in tqm ppt
 
Total Quality Management (TQM)
Total Quality Management (TQM)Total Quality Management (TQM)
Total Quality Management (TQM)
 
Tqm Final Ppt
Tqm Final PptTqm Final Ppt
Tqm Final Ppt
 
Total Quality Management (TQM)
Total Quality Management (TQM)Total Quality Management (TQM)
Total Quality Management (TQM)
 
Total quality management tools and techniques
Total quality management tools and techniquesTotal quality management tools and techniques
Total quality management tools and techniques
 
Tqm power point
Tqm power pointTqm power point
Tqm power point
 

Mais de Atit Patumvan

Iot for smart agriculture
Iot for smart agricultureIot for smart agriculture
Iot for smart agricultureAtit Patumvan
 
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตแบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตAtit Patumvan
 
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556Atit Patumvan
 
Chapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationChapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationAtit Patumvan
 
Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 1
การบริหารเชิงคุณภาพ ชุดที่ 1การบริหารเชิงคุณภาพ ชุดที่ 1
การบริหารเชิงคุณภาพ ชุดที่ 1Atit Patumvan
 
Write native iPhone applications using Eclipse CDT
Write native iPhone applications using Eclipse CDTWrite native iPhone applications using Eclipse CDT
Write native iPhone applications using Eclipse CDTAtit Patumvan
 
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic BindingChapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic BindingAtit Patumvan
 
OOP Chapter 8 : Inheritance
OOP Chapter 8 : InheritanceOOP Chapter 8 : Inheritance
OOP Chapter 8 : InheritanceAtit Patumvan
 
การตลาดโดยใช้พิกัดสถานที่เป็นฐาน
การตลาดโดยใช้พิกัดสถานที่เป็นฐานการตลาดโดยใช้พิกัดสถานที่เป็นฐาน
การตลาดโดยใช้พิกัดสถานที่เป็นฐานAtit Patumvan
 
OOP Chapter 7 : More on Classes
OOP Chapter 7 : More on ClassesOOP Chapter 7 : More on Classes
OOP Chapter 7 : More on ClassesAtit Patumvan
 
การจัดการธุรกิจแบบเหนือเมฆ
การจัดการธุรกิจแบบเหนือเมฆการจัดการธุรกิจแบบเหนือเมฆ
การจัดการธุรกิจแบบเหนือเมฆAtit Patumvan
 
OOP Chapter 6: Making Decisions
OOP Chapter 6: Making DecisionsOOP Chapter 6: Making Decisions
OOP Chapter 6: Making DecisionsAtit Patumvan
 
OOP Chapter 5 : Program Looping
OOP Chapter 5 : Program Looping OOP Chapter 5 : Program Looping
OOP Chapter 5 : Program Looping Atit Patumvan
 
OOP Chapter 4: Data Type and Expressions
OOP Chapter 4: Data Type and ExpressionsOOP Chapter 4: Data Type and Expressions
OOP Chapter 4: Data Type and ExpressionsAtit Patumvan
 
OOP Chapter 3: Classes, Objects and Methods
OOP Chapter 3: Classes, Objects and MethodsOOP Chapter 3: Classes, Objects and Methods
OOP Chapter 3: Classes, Objects and MethodsAtit Patumvan
 

Mais de Atit Patumvan (17)

Iot for smart agriculture
Iot for smart agricultureIot for smart agriculture
Iot for smart agriculture
 
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตแบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
 
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
 
Chapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationChapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computation
 
Media literacy
Media literacyMedia literacy
Media literacy
 
Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)
 
การบริหารเชิงคุณภาพ ชุดที่ 1
การบริหารเชิงคุณภาพ ชุดที่ 1การบริหารเชิงคุณภาพ ชุดที่ 1
การบริหารเชิงคุณภาพ ชุดที่ 1
 
Write native iPhone applications using Eclipse CDT
Write native iPhone applications using Eclipse CDTWrite native iPhone applications using Eclipse CDT
Write native iPhone applications using Eclipse CDT
 
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic BindingChapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
 
OOP Chapter 8 : Inheritance
OOP Chapter 8 : InheritanceOOP Chapter 8 : Inheritance
OOP Chapter 8 : Inheritance
 
การตลาดโดยใช้พิกัดสถานที่เป็นฐาน
การตลาดโดยใช้พิกัดสถานที่เป็นฐานการตลาดโดยใช้พิกัดสถานที่เป็นฐาน
การตลาดโดยใช้พิกัดสถานที่เป็นฐาน
 
OOP Chapter 7 : More on Classes
OOP Chapter 7 : More on ClassesOOP Chapter 7 : More on Classes
OOP Chapter 7 : More on Classes
 
การจัดการธุรกิจแบบเหนือเมฆ
การจัดการธุรกิจแบบเหนือเมฆการจัดการธุรกิจแบบเหนือเมฆ
การจัดการธุรกิจแบบเหนือเมฆ
 
OOP Chapter 6: Making Decisions
OOP Chapter 6: Making DecisionsOOP Chapter 6: Making Decisions
OOP Chapter 6: Making Decisions
 
OOP Chapter 5 : Program Looping
OOP Chapter 5 : Program Looping OOP Chapter 5 : Program Looping
OOP Chapter 5 : Program Looping
 
OOP Chapter 4: Data Type and Expressions
OOP Chapter 4: Data Type and ExpressionsOOP Chapter 4: Data Type and Expressions
OOP Chapter 4: Data Type and Expressions
 
OOP Chapter 3: Classes, Objects and Methods
OOP Chapter 3: Classes, Objects and MethodsOOP Chapter 3: Classes, Objects and Methods
OOP Chapter 3: Classes, Objects and Methods
 

Computer Programming Chapter 5 : Methods

  • 1. Computer Programming Chapter 5 : Methods Atit Patumvan Faculty of Management and Information Sciences Naresuna University
  • 2. 2 โปรแกรมย่อย (Sub Program) • โปรแกรมย่อย มีตําแหน่งในการเข้าถึงชุดคําสั่งเพียงตําแหน่งเดียว • โปรแกรมหลักจะหยุดการทํางาน เมื่อทําการเรียกใช้โปรแกรมย่อย และ • เมื่อโปรแกรมย่อยทํางานเสร็จสิ้นโปรแกรมหลักจะกลับมาทํางาน ต่อในชุดคําสั่งที่อยู่ต่อจาก คําสั่งเรียกใช้โปรแกรมย่อย • โปรแกรมย่อยสามารถแบ่งออกเป็น 2 กลุ่ม • Procedure เป็นโปรแกรมย่อยที่มีการรับค่าพารามิเตอร์จาก โปรแกรมหลัก • Function เป็นโปรแกรมย่อยที่มีการส่งค่าให้กับโปรแกรมหลัก Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 3. 3 การใหลของชุดคําสั่งในโปรแกรม Main Program statements Sub Program methods statements statements methods call statements statements statements Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 4. 4 ตัวอย่างการประยุกต์ใช้โปรแกรมย่อย 01: public class PrintStar { ***** 02: 03: public static void main(String[] args) { ***** 04: System.out.println("*****"); 05: System.out.println("*****"); ***** 06: System.out.println("*****"); 07: System.out.println("*****"); ***** 08: System.out.println("*****"); 09: } ***** 10: } : 03: public static void main(String[] args) { 04: for (int i = 1; i < 6; i++) { 05: System.out.println("*****"); 06: } 07: } : Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 5. 5 ตัวอย่างการประยุกต์ใช้โปรแกรมย่อย 01: public class PrintStar { 02: 03: 04: public static void main(String[] args) { for (int i = 0; i < 5; i++) { ***** 05: 06: } star(5); ***** 07: 08: } ***** 09: 10: public static void star(int x) { for (int i = 0; i < x; i++) { ***** 11: 12: } System.out.print("*"); ***** 13: System.out.println(); 14: } 15: } * : ** 03: public static void main(String[] args) { 04: for (int i = 0; i < 5; i++) { *** 05: star(i); 06: } **** 07: } : ***** Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 6. 6 Methods • ในภาษาที่เป็นโครงสร้าง เราสามารถสร้างหน่วยของโปรแกรม (procedure) เพื่อใช้ในการแก้ปัญหาที่ต้องการให้มีการประมวลผล ที่คล้ายๆกัน โดยผู้พัฒนาโปรแกรมไม่ต้องเขียนโปรแกรมซ้ําๆ หลายครั้ง • หน่วยของโปรแกรมที่สามารถส่งค่าออกมาเราจะเรียกว่าฟังก์ชัน (function) • ในภาษาเราจะเรียกหน่วยของโปรแกรมและฟังก์ชัน ด้วยเมธอส (method) Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 7. 7 Method Invocation • การเรียกใช้ Method ทําได้หลายแบบ • Method ที่ไม่ส่งค่ากลับ: จะต้องถูกเรียกจากตําแหน่งประโยค • Method ที่มีการการส่งค่ากลับ: จะต้องถูกเรียกจาก term ภายใน expression • โครงสร้างการเรียกใช้เป็นดังนี้ <method_name> ([argument list]) 01: public class MethodInvocation { 02: 03: public static void main(String[] args) { 04: method(); 05: } 06: 07: public static void method() { 08: System.out.println("sub program is called"); 09: } 10: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 8. 8 การเรียก Method ที่มีการส่งค่ากลับ 01: public class FunctionInvocation { 02: 03: public static void main(String[] args) { 04: int x = 7, y = 5; 05: int z = add(x,y); 06: System.out.println(z); 07: } 08: 09: public static int add(int a, int b) { 10: int c = a + b; 11: return c; 12: } 13: } send value (parameter) caller method calee method return value Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 9. 9 Method Overloading • การที่เมธอสมีชื่อเหมือนกันได้มากกว่าหนึ่งเมธอส 01: public class MethoadOverload { 02: 03: public static void main(String[] args) { 04: int x = 7, y = 5; 05: double z = 5.0; 06: 07: System.out.println(x / y); 08: 09: System.out.println(div(x, y)); 10: 11: System.out.println(div(x, z)); 12: } 13: 14: public static double div(int a, int b) { 15: double c = a / (double) b; 16: return c; 17: } 18: 19: public static double div(int a, double b) { 20: double c = a / b; 21: return c; 22: } 23: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 10. 10 Parameter Passing: Pass by Value 01: public class PassByValue { 02: 03: public static void main(String[] args) { 04: String name = "alice"; 05: method(name); 06: System.out.println(name); 07: } 08: 09: public static void method(String arg) { 10: arg = "bob" ; 11: } 12: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 11. 11 Parameter Passing: Pass by Reference 01: public class PassByReference { 02: 03: public static void main(String[] args) { 04: Person p = new Person(); 05: p.setName("alice"); 06: method(p); 07: System.out.println(p.getName()); 08: } 09: 10: public static void method(Person arg) { 11: arg.setName("bob"); 12: } 13: } 14: 15: class Person { 16: 17: private String name; 18: 19: public String getName() { 20: return name; 21: } 22: 23: public void setName(String name) { 24: this.name = name; 25: } 26: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 12. 12 Scope of Visibility static instance method block block method block Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University