SlideShare a Scribd company logo
บทที่ 6
การพัฒ นาโปรแกรมภาษา
    จาวาเชิง ออปเจ็ค
  (Object Oriented Java
    Programming)

   อ.ธนิศ า เครือ ไวศยวรรณ
  คณะเทคโนโลยีส ารสนเทศ
วัต ถุป ระสงค์
 อธิบ ายเมธอด       และการเรีย กใช้เ มธอด
 อธิบ ายคีย เ วิร ์ด ทีใ ช้เ ป็น
             ์          ่           modifier ของเมธอด
 แนะนำา การเขีย นโปรแกรมภาษาจาวาเชิง ออปเจ็ค

 อธิบ ายคุณ ลัก ษณะเด่น ของโปรแกรมเชิง ออปเจ็ค

 อธิบ ายความหมายของ                Constructor
การเขีย นโปรแกรมภาษาจาวาเชิง
           ออปเจ็ค
 ตัว อย่า งโปรแกรมในบททีผ ่า น ๆมา
                         ่              บางครั้ง จะ
 เขีย นทุก คำา สัง อยู่ภ ายใน เมธอดทีช ื่อ main() ซึง
                 ่                    ่                ่
 ไม่ไ ด้ใ ช้ห ลัก การเชิง ออปเจ็ค และไม่เ หมาะทีจ ะนำา
                                                  ่
 ไปใช้ใ นทางปฏิบ ัต ิเ มือ โปรแกรมมีข นาดใหญ่ข น
                           ่                        ึ้
 การเขีย นโปรแกรมเชิง ออปเจ็ค ทีด ีจ ะต้อ งมีก าร
                                 ่
 กำา หนดเมธอดต่า งๆให้ก ับ คลาส
ตัว อย่า งโปรแกรมที่ท ุก คำา สัง อยู่ใ น
                               ่
            เมธอด main()
  public class AllInMain {
   public class AllInMain {
          public static void main(String args[]) {
           public static void main(String args[]) {
                  double score = Math.random()*100;
                   double score = Math.random()*100;
                  if (score >= 80) {
                   if (score >= 80) {
                          System.out.println("Grade is A");
                           System.out.println("Grade is A");
                  } else if (score >= 70) {
                   } else if (score >= 70) {
                          System.out.println("Grade is B");
                           System.out.println("Grade is B");
                  } else if (score >= 60){
                   } else if (score >= 60){
                          System.out.println("Grade is C");
                           System.out.println("Grade is C");
                  } else if (score >= 50){
                   } else if (score >= 50){
                          System.out.println("Grade is D");
                           System.out.println("Grade is D");
                  } else {
                   } else {
                          System.out.println("Grade is F");
                           System.out.println("Grade is F");
                  }}
          }}
  }}
ตัว อย่า งโปรแกรมที่ใ ช้ห ลัก การเชิง
              ออปเจ็ค
  public class NumericalClass {{
   public class NumericalClass
           public void calculate() {{
            public void calculate()
                    double score == Math.random()*100;
                     double score    Math.random()*100;
                    if (score >= 80) {{
                     if (score >= 80)
                              System.out.println("Grade is A");
                               System.out.println("Grade is A");
                    }} else if (score >= 70) {{
                        else if (score >= 70)
                              System.out.println("Grade is B");
                               System.out.println("Grade is B");
                    }} else if (score >= 60){
                        else if (score >= 60){
                              System.out.println("Grade is C");
                               System.out.println("Grade is C");
                    }} else if (score >= 50){
                        else if (score >= 50){
                              System.out.println("Grade is D");
                               System.out.println("Grade is D");
                    }} else {{
                        else
                              System.out.println("Grade is F");
                               System.out.println("Grade is F");
                    }}
           }}
  }}

  -----------------------------------------------------------------
   -----------------------------------------------------------------
  -------------------------
   -------------------------
  public class MyMain {
   public class MyMain {
           public static void main(String args[]) {{
            public static void main(String args[])
                    NumericalClass obj == new NumericalClass();
                     NumericalClass obj    new NumericalClass();
                    obj.calculate();
                     obj.calculate();
ตัว อย่า งโปรแกรมที่ท ุก เมธอดอยู่
          ในคลาสเดีย วกัน
public class NumericalClassV1 {
 public class NumericalClassV1 {
        public void calculate() {
         public void calculate() {
                double score = Math.random()*100;
                 double score = Math.random()*100;
                if (score >= 80) {
                 if (score >= 80) {
                        System.out.println("Grade is A");
                         System.out.println("Grade is A");
                } else if (score >= 70) {
                 } else if (score >= 70) {
                        System.out.println("Grade is B");
                         System.out.println("Grade is B");
                } else if (score >= 60){
                 } else if (score >= 60){
                        System.out.println("Grade is C");
                         System.out.println("Grade is C");
                } else if (score >= 50){
                 } else if (score >= 50){
                        System.out.println("Grade is D");
                         System.out.println("Grade is D");
                } else {
                 } else {
                        System.out.println("Grade is F");
                         System.out.println("Grade is F");
                }}
        }}
        public static void main(String args[]) {
         public static void main(String args[]) {
                NumericalClassV1 obj = new NumericalClassV1();
                 NumericalClassV1 obj = new NumericalClassV1();
                obj.calculate();
                 obj.calculate();
        }}
}}
รูป แบบการเรีย กใช้เ มธอด
 รูป แบบของคำา สั่ง ทีม ก ารเรีย กใช้เ มธอดเป็น ดัง นี้
                       ่ ี
          obj.methodName([arguments]);
 arguments   อาจจะเป็น ข้อ มูล ค่า คงทีห รือ ตัว แปร
                                        ่
 ชนิด ข้อ มูล ของ arguments
                          ที่ใ ช้ใ นการเรีย กเมธอด
 จะต้อ งสอดคล้อ งกัน กับ ชนิด ข้อ มูล ของ arguments
 ของเมธอด
การเรีย กใช้เ มธอด
 เมธอดทีก ำา หนดขึ้น ในคลาสใดๆสามารถเรีย กใช้
         ่
 งานได้ส องรูป แบบคือ
  • การเรีย กใช้ง านจากคลาสที่ต ่า งกัน
  • การเรีย กใช้ง านภายในคลาสเดีย วกัน

 การเรีย กใช้เ มธอดจากคลาสทีต ่า งกัน
                             ่         จะต้อ งมี
 การสร้า งออปเจ็ค ของคลาสทีม เ มธอดทีจ ะถูก เรีย ก
                              ่ ี       ่
 ใช้ง านก่อ น จึง จะสามารถเรีย กใช้เ มธอดได้
 การเรีย กใช้เ มธอดภายในคลาสเดีย วกัน      ในบาง
 กรณีจ ะสามารถทำา ได้โ ดยไม่จ ำา เป็น ต้อ งสร้า งออป
 เจ็ค ของคลาสขึ้น มาก่อ น และสามารถเรีย กเมธอด
 ได้ท ก เมธอด
      ุ
ตัว อย่า งโปรแกรมที่ม ีก ารเรีย กใช้
       เมธอดในคลาสเดีย วกัน
public class NumericalClassV2 {
 public class NumericalClassV2 {
        public void calculate() {
         public void calculate() {
                double score = Math.random()*100;
                 double score = Math.random()*100;
                if (score >= 80) {
                 if (score >= 80) {
                        System.out.println("Grade is A");
                         System.out.println("Grade is A");
                } else if (score >= 70) {
                 } else if (score >= 70) {
                        System.out.println("Grade is B");
                         System.out.println("Grade is B");
                } else if (score >= 60){
                 } else if (score >= 60){
                        System.out.println("Grade is C");
                         System.out.println("Grade is C");
                } else if (score >= 50){
                 } else if (score >= 50){
                        System.out.println("Grade is D");
                         System.out.println("Grade is D");
                } else {
                 } else {
                        System.out.println("Grade is F");
                         System.out.println("Grade is F");
                }}
        }}
        public void callMethod() {
         public void callMethod() {
                calculate();
                 calculate();
        }}
}}
การส่ง ผ่า น argument
 กรณีท ี่เ มธอด มี argument       ทีจ ะรับ ค่า เพือ นำา ไปใช้ใ น
                                     ่             ่
  เมธอด อาทิเ ช่น
      public void setGPA(double GPA) {
            ...
      }
 คำา สั่ง ทีเ รีย กใช้เ มธอดนี้
             ่                   จะต้อ งส่ง argument ทีม ี
                                                       ่
  ชนิด ข้อ มูล เป็น   double   ไป พร้อ มกับ ชื่อ เมธอด เช่น
  setGPA(3.0);
ตัว อย่า งโปรแกรม
public class Student {
 public class Student {
        String id;
         String id;
        String name;
         String name;
        double gpa;
         double gpa;
        public void setGPA(double GPA) {
         public void setGPA(double GPA) {
                gpa = GPA;
                 gpa = GPA;
        }}
        public double getGPA() {
         public double getGPA() {
                return gpa;
                 return gpa;
        }}
}}
--------------------------------------------------------------
 --------------------------------------------------------------
public class MyMain2 {
 public class MyMain2 {
        public static void main(String args[]) {
         public static void main(String args[]) {
                Student s1 = new Student();
                 Student s1 = new Student();
                s1.setGPA(3.0);
                 s1.setGPA(3.0);
        }}
}}
argument ของเมธอด
 argumentของเมธอดจะมีช นิด ข้อ มูล เป็น สอง
 แบบตามชนิด ข้อ มูล ของ ตัว แปรดัง นี้
  • argument         ที่ม ีช นิด ข้อ มูล แบบพื้น ฐาน
  • argument         ที่ม ีช นิด ข้อ มูล แบบอ้า งอิง
 ในกรณีข อง    argument ทีม ช นิด ข้อ มูล แบบพืน
                            ่ ี                 ้
 ฐาน เราสามารถทีจ ะส่ง ค่า คงทีข ้อ มูล ตัว แปร
                       ่        ่
 หรือ นิพ จน์ใ ห้ก ับ argument ได้ ตัว อย่า งเช่น
  • ค่า คงที่ข ้อ มูล   เช่น
        s1.setGPA(3.0);
  • ตัว แปร   เช่น
        double x = 3.0;
        s1.setGPA(x);
  • นิพ จน์   เช่น
        s1.setGPA(3.0+0.05);
argument ที่ม ีช นิด ข้อ มูล แบบ
            อ้า งอิง
 เราจะต้อ งส่ง ออปเจ็ค ทีม ช นิด ข้อ มูล ทีส อดคล้อ งไป
                          ่ ี               ่
 เท่า นัน
        ้
 ยกเว้น กรณีท ี่argument นัน มีช นิด ข้อ มูล เป็น
                             ้
 String   ซึง ในกรณีน จ ะสามารถส่ง ข้อ มูล ค่า คงทีไ ด้
            ่         ี้                           ่
ตัว อย่า งโปรแกรมที่มีก ารส่ง
   argument ที่เ ป็น ออปเจ็ค
import java.util.*;
 import java.util.*;

public class StudentV1 {
 public class StudentV1 {
     String id;
      String id;
     String name;
      String name;
     Date dob;
      Date dob;
     public void setDOB(Date d) {
      public void setDOB(Date d) {
            dob = d;
             dob = d;
     }}
     public Date getDOB() {
      public Date getDOB() {
            return dob;
             return dob;
     }}
}}
-----------------------------------------------------------------
 -----------------------------------------------------------------
-------------------------
 -------------------------
public class TestStudentV1 {
 public class TestStudentV1 {
     public static void main(String args[]) {
      public static void main(String args[]) {
            StudentV1 s1 = new StudentV1();
             StudentV1 s1 = new StudentV1();
            Date d1 = new Date(16,12,1980);
             Date d1 = new Date(16,12,1980);
            s1.setDOB(d1);
             s1.setDOB(d1);
ชนิด ข้อ มูล และจำา นวนของ
              argument
 ชนิด ข้อ มูล ของ argument ที่จ ะส่ง ผ่า นไปยัง
 เมธอดไม่จ ำา เป็น ทีจ ะต้อ งเป็น ชนิด ข้อ มูล เดีย วกัน
                      ่
 แต่ต ้อ งเป็น ชนิด ข้อ มูล ทีส ามารถแปลงข้อ มูล ให้
                              ่
 กว้า งขึ้น ได้โ ดยอัต โนมัต ิ
 เมธอดใดๆอาจมี    argument สำา หรับ รับ ค่า
 มากกว่า หนึง ตัว แต่ก ารเรีย กใช้เ มธอดเหล่า นีจ ะ
             ่                                   ้
 ต้อ งส่ง argument ทีม ช นิด ข้อ มูล ทีส อดคล้อ งกัน
                       ่ ี             ่
 และมีจ ำา นวนเท่า กัน
ตัว อย่า งโปรแกรมที่มีก ารส่ง
   argument จำา นวนสองตัว
public class NumericalSample {
 public class NumericalSample {
        public void calMax(int i, double d) {
         public void calMax(int i, double d) {
              if (i > d) {
               if (i > d) {
                     System.out.println("Max = "+i);
                      System.out.println("Max = "+i);
              } else {
               } else {
                     System.out.println("Max = "+d);
                      System.out.println("Max = "+d);
              }}
        }}
        public static void main(String args[]) {
         public static void main(String args[]) {
              NumericalSample obj = new NumericalSample();
               NumericalSample obj = new NumericalSample();
              obj.calMax(3,4.0);
               obj.calMax(3,4.0);
        }}
}}
การเปลีย นแปลงค่า ของ
              ่
            argument
 การส่ง argument ทีมีช นิด ข้อ มูล เป็น แบบพื้น ฐาน
                        ่
  หากมีก ารเปลี่ย นแปลงค่า ของ argument
 ภายในเมธอดทีถ ูก เรีย กใช้ง าน จะไม่ม ผ ลทำา ให้
                 ่                        ี
 ค่า ของ argument ทีส ่ง ไปเปลี่ย นค่า ไปด้ว ย
                      ่
 การส่ง argument ทีม ช นิด ข้อ มูล เป็น แบบอ้า งอิง
                        ่ ี
 จะเป็น การส่ง ตำา แหน่ง อ้า งอิง ของออปเจ็ค ไปให้
 กับ เมธอดทีถ ูก เรีย กใช้ง าน ดัง นั้น การ
              ่
 เปลี่ย นแปลงค่า ของคุณ ลัก ษณะขอ งออปเจ็ค จะมี
 ผลทำา ให้ค ่า ของคุณ ลัก ษณะของออปเจ็ค ทีส ่ง ไป
                                             ่
 เปลี่ย นไปด้ว ย
ตัว อย่า งโปรแกรมที่แ สดงการ
   เปลี่ย นแปลงค่า ของ argument
import java.util.*;
 import java.util.*;

public class ArgumentPassing {
 public class ArgumentPassing {
        public void method1(int x) {
         public void method1(int x) {
                x = 3;
                 x = 3;
        }}
        public void method2(Date d) {
         public void method2(Date d) {
                d.setDate(1);
                 d.setDate(1);
                d.setMonth(1);
                 d.setMonth(1);
                d.setYear(2002);
                 d.setYear(2002);
        }}
        public int method3() {
         public int method3() {
                return 0;
                 return 0;
        }}
ตัว อย่า งโปรแกรมที่แ สดงการ
     เปลี่ย นแปลงค่า ของ argument
       public static void main(String args[]) {
        public static void main(String args[]) {
               int x = 4;
                int x = 4;
               ArgumentPassing obj = new ArgumentPassing();
                ArgumentPassing obj = new ArgumentPassing();
               Date d1 = new Date(16,12,1980);
                Date d1 = new Date(16,12,1980);
               obj.method1(x);
                obj.method1(x);
               System.out.println("x = "+x);
                System.out.println("x = "+x);
               obj.method2(d1);
                obj.method2(d1);
               System.out.println("Date = "+d1);
                System.out.println("Date = "+d1);
               obj.method3();
                obj.method3();
       }}
}}
การรับ ค่า ที่ส ง กลับ มาจากเมธอด
                  ่
 เมธอดใดๆของคลาสสามารถทีจ ะมีค ่า ทีส ่ง กลับ มา
                         ่           ่
  ได้ ซึ่ง ชนิด ข้อ มูล ของค่า ทีจ ะส่ง กลับ อาจเป็น ชนิด
                                 ่
  ข้อ มูล แบบพืน ฐาน หรือ เป็น ชนิด ข้อ มูล แบบอ้า งอิง
                 ้
 เมธอดทีม ค ่า ทีจ ะส่ง กลับ มาจะต้อ งมีค ำา สั่ง return
         ่ ี      ่
  ซึ่ง จะระบุค ่า ทีส ่ง กลับ โดยมีร ูป แบบดัง นี้
                    ่
           return value;

 คำา สั่ง ทีเ รีย กใช้เ มธอด
             ่            อาจจะรับ ค่า ทีส ่ง กลับ มา
                                             ่
  เก็บ ไว้ใ นตัว แปรหรือ เป็น ตัว ถูก ดำา เนิน การใน
  นิพ จน์ ตัว อย่า งเช่น
           double d = getGPA();
           System.out.println("GPA:"+getGPA());
modifier ของเมธอด
 modifier       ของเมธอดประกอบด้ว ย
  • access modifier
  • static
  • abstract
  • synchronized
  • final



 access  modifier ใช้เ พื่อ ระบุร ะดับ การเข้า ถึง
 โดยมีค ีย เ วิร ์ด ต่า งๆดัง นี้
           ์
  •   public
  •   protected
  •   private
  •   default (ไม่ร ะบุุค ีย ์เ วิร ์ด ใดๆ)
ตัว อย่า งโปรแกรมที่แ สดงการประกาศ
          เมธอดและคุณ ลัก ษณะ
public class PrivateStudent {
 public class PrivateStudent {
    private String id;
     private String id;
    private String name;
     private String name;
    private double gpa;
     private double gpa;
    public void setDetails(String ID,String n,double GPA) {
     public void setDetails(String ID,String n,double GPA) {
        id = ID;
         id = ID;
        name = n;
         name = n;
        gpa = GPA;
         gpa = GPA;
    }}
    public void showDetails() {
     public void showDetails() {
        System.out.println("ID: "+id);
         System.out.println("ID: "+id);
        System.out.println("Name: "+name);
         System.out.println("Name: "+name);
        System.out.println("GPA: "+gpa);
         System.out.println("GPA: "+gpa);
    }}
}}
ตัว อย่า งโปรแกรมที่แ สดงการประกาศ
          เมธอดและคุณ ลัก ษณะ
public class TestPrivateStudent {
 public class TestPrivateStudent {
     public static void main(String args[]) {
      public static void main(String args[]) {
           PrivateStudent ps = new PrivateStudent();
            PrivateStudent ps = new PrivateStudent();
           /*
            /* ps.id = "12345";
                 ps.id = "12345";             illegal
                                               illegal
                ps.name = "Thana";
                 ps.name = "Thana";           illegal
                                               illegal
                ps.gpa = 3.25;
                 ps.gpa = 3.25;               illegal */
                                               illegal */
           ps.setDetails("12345","Thana",3.25);
            ps.setDetails("12345","Thana",3.25);
           ps.showDetails();
            ps.showDetails();
     }}
}}
ตัว อย่า งโปรแกรม
public class ThisStudent {
 public class ThisStudent {
        private String id;
         private String id;
        private String name;
         private String name;
        private double gpa;
         private double gpa;
        public void setDetails(String id,String name, double gpa) {
         public void setDetails(String id,String name, double gpa) {
                this.id = id;
                 this.id = id;
                this.name = name;
                 this.name = name;
                this.gpa = gpa;
                 this.gpa = gpa;
        }}
        public void showDetails() {
         public void showDetails() {
                System.out.println("ID: "+this.id);
                 System.out.println("ID: "+this.id);
                System.out.println("Name: "+name);
                 System.out.println("Name: "+name);
                System.out.println("GPA: "+gpa);
                 System.out.println("GPA: "+gpa);
        }}
}}
การเขีย นโปรแกรมโดยใช้ห ลัก
        การของการห่อ หุ้ม
 คุณ ลัก ษณะเด่น ของโปรแกรมเชิง ออปเจ็ค มีอ ยู่
 สามอย่า งคือ การห่อ หุม การสืบ ทอด และการมีไ ด้
                       ้
 หลายรูป แบบ
 ข้อ ดีข องการห่อ หุม ประการหนึง คือ การซ่อ นเร้น
                     ้          ่
 ข้อ มูล
 หลัก การห่อ หุม ของออปเจ็ค ทำา ได้ด ัง นี้
                ้
   • กำา หนดคุณ ลัก ษณะของออปเจ็ค ให้มี         modifier เป็น
     private เพื่อ ซ่อ นไม่ใ ห้อ อปเจ็ค อื่น ๆเรีย กใช้ไ ด้
   • กำา หนดเมธอดของออปเจ็ค ที่ต ้อ งการให้อ อปเจ็ค อื่น ๆ
     เรีย กใช้ใ ห้มี modifier เป็น public
ตัว อย่า งโปรแกรมที่ไ ม่ไ ด้ใ ช้ห ลัก
         การของการห่อ หุ้ม
public class Student {
 public class Student {
        String ID;
         String ID;
        String name;
         String name;
        public double gpa;
         public double gpa;
}}
-----------------------------------------------------------------------
 -----------------------------------------------------------------------
---------------------------
 ---------------------------
public class NoEncapDemo {
 public class NoEncapDemo {
        public static void main(String args[]) {
         public static void main(String args[]) {
                Student s1 = new Student();
                 Student s1 = new Student();
                double temp = Double.parseDouble(args[0]);
                 double temp = Double.parseDouble(args[0]);
                if ((temp<0) || (temp>4.00)) {
                 if ((temp<0) || (temp>4.00)) {
                        System.out.println("Incorrect Format!");
                         System.out.println("Incorrect Format!");
                } else {
                 } else {
                        s1.gpa = temp;
                         s1.gpa = temp;
                        System.out.println("GPA: "+s1.gpa);
                         System.out.println("GPA: "+s1.gpa);
                }}
        }}
}}
เมธอดแบบ setter
 เมธอดแบบ      accessor แบ่ง ออกได้เ ป็น สอง
 ประเภทคือ
  • เมธอดแบบ    setter
  • เมธอดแบบ    getter
 เมธอดแบบ      setter จะใช้ใ นการกำา หนดค่า ของ
 คุณ ลัก ษณะ
 โดยทัว ไปชื่อ ของเมธอดแบบ
       ่                          setter จะขึ้น ต้น
 ด้ว ยคำา ว่า set แล้ว ตามด้ว ยชื่อ ของคุณ ลัก ษณะ
 ซึ่ง มีร ูป แบบดัง นี้
          public void setAttributeName(dataType arg) {
           attributeName = arg;
      }
ตัว อย่า งโปรแกรมที่ใ ช้ห ลัก การ
            ของการห่อ หุ้ม
public class Student {{
 public class Student
         String ID;
          String ID;
         String name;
          String name;
         private double gpa;
          private double gpa;
         public void setGPA(double GPA) {{
          public void setGPA(double GPA)
                  if ((GPA<0) || (GPA>4.00)) {{
                   if ((GPA<0) || (GPA>4.00))
                            System.out.println("Incorrect Format!");
                             System.out.println("Incorrect Format!");
                  }} else {{
                      else
                            gpa == GPA;
                             gpa    GPA;
                  }}
         }}
         public double getGPA() {{
          public double getGPA()
                  return gpa;
                   return gpa;
         }}
}}

-----------------------------------------------------------------------
 -----------------------------------------------------------------------
---------------------------
 ---------------------------
public class EncapDemo {
public class EncapDemo {
        public static void main(String args[]) {{
         public static void main(String args[])
                 Student s1 == new Student();
                  Student s1    new Student();
                 double temp == Double.parseDouble(args[0]);
                  double temp    Double.parseDouble(args[0]);
                 s1.setGPA(temp);
                  s1.setGPA(temp);
                 System.out.println("GPA: "+s1.getGPA());
                  System.out.println("GPA: "+s1.getGPA());
เมธอดแบบ getter
 เมธอดแบบ     getter จะใช้ใ นการเรีย กค่า ของ
 คุณ ลัก ษณะ
 โดยทัว ไปชื่อ ของเมธอดแบบ
       ่                        getter จะขึน ต้น
                                              ้
 ด้ว ยคำา ว่า get แล้ว ตามด้ว ยชื่อ ของคุณ ลัก ษณะ
 ซึ่ง มีร ูป แบบดัง นี้
         public dataType getAttributeName() {
          return attributeName;
     }
ตัว อย่า งโปรแกรมที่ใ ช้ห ลัก การ
            ของการห่อ หุ้ม
public class EncapStudent {
 public class EncapStudent {
        private String id;
         private String id;
        private String name;
         private String name;
        private double gpa;
         private double gpa;
        public void setID(String ID) {
         public void setID(String ID) {
                id = ID;
                 id = ID;
        }}
        public void setName(String n) {
         public void setName(String n) {
                name = n;
                 name = n;
        }}
        public void setGPA(double GPA) {
         public void setGPA(double GPA) {
                if ((GPA<0) || (GPA>4.00)) {
                 if ((GPA<0) || (GPA>4.00)) {
                        System.out.println("Incorrect Format!");
                         System.out.println("Incorrect Format!");
                } else {
                 } else {
                        gpa = GPA;
                         gpa = GPA;
                }}
        }}
ตัว อย่า งโปรแกรมที่ใ ช้ห ลัก การ
           ของการห่อ หุ้ม
     public String getID() {
      public String getID() {
        return id;
         return id;
     }}
     public String getName() {
      public String getName() {
        return name;
         return name;
     }}
     public double getGPA() {
      public double getGPA() {
             return gpa;
              return gpa;
     }}
}}
คีย ์เ วิร ์ด   this

 คีย เวิร ์ด this   หมายถึง ออปเจ็ค ของตัว เอง
 เราสามารถทีจ ะเรีย กใช้เ มธอดหรือ คุณ ลัก ษณะ
             ่
  ภายในคลาสได้โ ดยใช้                 คีย เ วิร ์ด
                                          ์          this   ซึ่ง มีร ูป
  แบบดัง นี้
            this.methodName();
            this.attributeName

 โดยทัว ไปเราจะไม่ใ ช้ค ีย เ วิร ์ด this
       ่                    ์                   ในคำา สั่ง
  ยกเว้น ในกรณีท จ ำา เป็น
                 ี่
การเขีย นโปรแกรมโดยใช้ห ลัก
      การของการสืบ ทอด
 ข้อ ดีข องการสืบ ทอดคือ การนำา คลาสทีม อ ยูแ ล้ว มา
                                        ่ ี ่
 ใช้ใ หม่โ ดยการเพิม เติม คุณ ลัก ษณะหรือ เมธอดใน
                   ่
 คลาสใหม่
 การพัฒ นาคลาสขึ้น ใหม่ท ช ื่อ GradStudent
                          ี่
 สามารถทีจ ะเลือ กวิธ ีก ารได้ส องแบบคือ
         ่
  • สร้า งคลาสขึ้น มาใหม่โ ดยไม่อ ้า งอิง กับ คลาสเดิม ที่ช ื่อ
    Student
  • สร้า งคลาสที่ส ืบ ทอดมาจากคลาสเดิม ที่ช ื่อ Student
ตัว อย่า งโปรแกรม แสดงคลาสที่ช ื่อ
                         Student
 public class Student {
  public class Student {
         private String id;
          private String id;
         private String name;
          private String name;
         private double gpa;
          private double gpa;
         public void setID(String ID) {
          public void setID(String ID) {
                 id = ID;
                  id = ID;
         }}
         public void setName(String n) {
          public void setName(String n) {
                 name = n;
                  name = n;
         }}
         public void setGPA(double GPA) {
          public void setGPA(double GPA) {
                 gpa = GPA;
                  gpa = GPA;
         }}
         public void showDetails() {
          public void showDetails() {
                 System.out.println("ID: "+id);
                  System.out.println("ID: "+id);
                 System.out.println("Name: "+name);
                  System.out.println("Name: "+name);
                 System.out.println("GPA: "+gpa);
                  System.out.println("GPA: "+gpa);
         }}
 }}
ตัว อย่า งโปรแกรมที่ไ ม่ไ ด้ใ ช้ห ลัก
        การของการสืบ ทอด
public class GradStudent {
 public class GradStudent {
        private String id;
         private String id;
        private String name;
         private String name;
        private double gpa;
         private double gpa;
        private String thesisTitle;
         private String thesisTitle;
        private String supervisor;
         private String supervisor;
        public void setID(String ID) {
         public void setID(String ID) {
                id = ID;
                 id = ID;
        }}
        public void setName(String n) {
         public void setName(String n) {
                name = n;
                 name = n;
        }}
        public void setGPA(double GPA) {
         public void setGPA(double GPA) {
                gpa = GPA;
                 gpa = GPA;
        }}
ตัว อย่า งโปรแกรมที่ไ ม่ไ ด้ใ ช้ห ลัก
         การของการสืบ ทอด
     public void setThesisTitle(String t) {
      public void setThesisTitle(String t) {
             thesisTitle = t;
              thesisTitle = t;
     }}
     public void setSupervisor(String s) {
      public void setSupervisor(String s) {
             supervisor = s;
              supervisor = s;
     }}
     public void showThesis() {
      public void showThesis() {
             System.out.println("ThesisTitle: "+thesisTitle);
              System.out.println("ThesisTitle: "+thesisTitle);
             System.out.println("Supervisor: "+supervisor);
              System.out.println("Supervisor: "+supervisor);
     }}
}}
ตัว อย่า งโปรแกรมที่ใ ช้ห ลัก การ
            ของการสืบ ทอด
public class GradStudent extends Student {
 public class GradStudent extends Student {
        private String thesisTitle;
         private String thesisTitle;
        private String supervisor;
         private String supervisor;
        public void setThesisTitle(String t) {
         public void setThesisTitle(String t) {
                thesisTitle = t;
                 thesisTitle = t;
        }}
        public void setSupervisor(String s) {
         public void setSupervisor(String s) {
                supervisor = s;
                 supervisor = s;
        }}
        public void showThesis() {
         public void showThesis() {
                System.out.println("ThesisTitle: "+thesisTitle);
                 System.out.println("ThesisTitle: "+thesisTitle);
                System.out.println("Supervisor: "+supervisor);
                 System.out.println("Supervisor: "+supervisor);
        }}
}}
รูป แสดงหลัก การของการสืบ ทอด
ตัว อย่า งการสืบ ทอดทีไ ม่ถ ูก ต้อ ง
                        ่
 เราสามารถทีต รวจสอบหลัก การของการสืบ ทอด
             ่
 ด้ว ยคำา ว่า is a

  public class Shirt {
         char size;
         float price;
  }

  public class Skirt extends Shirt {
         boolean long;
  }
ตัว อย่า งการสืบ ทอดที่ถ ูก ต้อ ง
 public class Clothing {
       char size;
       float price;
}

public class Shirt extends Clothing {
}

public class Skirt extends Clothing {
       boolean long;
}
คีย ์เ วิร ์ด   protected

 คุณ ลัก ษณะหรือ เมธอดของ         superclass ทีม ี
                                                  ่
  modifier เป็น แบบ private        จะทำา ให้ subclass ไม่
  สามารถทีจ ะเรีย กใช้ไ ด้
          ่
 ภาษาจาวากำา หนดให้ม ี     access modifier ทีช ื่อ
                                              ่
  protected ซึง จะทำา ให้ subclass สามารถเรีย กใช้
              ่
  เมธอดหรือ คุณ ลัก ษณะของ superclass ได้
 ตัว อย่า งเช่น
          protected String name;
คีย ์เ วิร ์ด   protected

 ภาษาจาวากำา หนดให้ม ี   access modifier ทีช ื่อ
                                              ่
 protected ซึ่ง จะทำา ให้ค ลาสทีเ ป็น subclass
                                  ่
 สามารถทีจ ะเรีย กใช้เ มธอดหรือ คุณ ลัก ษณะของ
          ่
 superclass ได้ แม้ว ่า จะอยูต ่า งคลาสหรือ อยู่ต ่า ง
                             ่
 แพคเก็จ กัน ก็ต าม
                           คลาสที่
                           อยู่ใ น
                 คลาส               คลาสที่
                            แพจ               คลาส
                เดีย วกั            เป็น su
                            เก็จ               ใดๆ
                   น                bclass
                           เดีย วกั
                              น


      public       √           √      √        √
ตัว อย่า งโปรแกรม
     public class Student {
      public class Student {
      protected String id;
       protected String id;
      protected String name;
       protected String name;
      protected double gpa;
       protected double gpa;
      public void setDetails(String ID,String n,double GPA) {
       public void setDetails(String ID,String n,double GPA) {
                id = ID;
                 id = ID;
               name = n;
                name = n;
               gpa = GPA;
                gpa = GPA;
      }}
      public void showDetails() {
       public void showDetails() {
               System.out.println("ID: "+id);
                System.out.println("ID: "+id);
               System.out.println("Name: "+name);
                System.out.println("Name: "+name);
               System.out.println("GPA: "+gpa);
                System.out.println("GPA: "+gpa);
      }}
}}
คลาสที่ช ื่อ       Object

 ภาษาจาวาได้ก ำา หนดให้ค ลาสใดๆ      สามารถจะ
  สืบ ทอดคลาสอื่น ได้เ พีย งคลาสเดีย วเท่า นัน
                                             ้
 คลาสทุก คลาสในภาษาจาวาถ้า ไม่ไ ด้ส ืบ ทอดจาก
  คลาสใดเลยจะถือ ว่า คลาสนั้น สืบ ทอดจากคลาสที่
  ชื่อ Object
 ตัว อย่า งเช่น
           public class Student extends Object {
            ...
       }

 คลาสทีช ื่อ Object
        ่              จะมีเ มธอดทีส ำา คัญ คือ
                                   ่
           public String toString()
   และ      public boolean equals(Object o)
คีย ์เ วิร ์ด   super

   super    เป็น คีย เ วิร ์ด ทีใ ช้ใ นการอ้า งอิง ถึง
                      ์          ่
    superclass เพือ ทีจ ะเรีย กใช้ เมธอดหรือ
                          ่ ่
    constructor ของ superclass โดยมีร ูป แบบคำา
    สั่ง ดัง นี้
            super.methodName([arguments])

 ตัว อย่า งเช่น    คลาส GradStudent อาจมีเ มธอดทีช อ่ ื่
    showDetails()   โดยมีค ำา สัง ทีเ รีย กใช้เ มธอด
                                ่ ่
    showDetails()   ของคลาส Student และ showThesis()
    ของคลาส     GradStudent
            public void showDetails() {
                  super.showDetails();
                  showThesis();
            }
การเขีย นโปรแกรมโดยใช้ห ลัก การของ
         การมีไ ด้ห ลายรูป แบบ
 การมีไ ด้ห ลายรูป แบบหมายถึง     คุณ สมบัต ิข องออป
 เจ็ค ของคลาสทีต ่า งกัน สามารถตอบสนองต่อ
                ่
 เมธอดเดีย วกัน ในวิธ ีก ารที่ต า งกัน ได้ ซึ่ง หมายถึง
                                ่
 การเขีย นเมธอดแบบ overridden และการใช้
 Dynamic Binding
 การเขีย นเมธอดแบบ         overridden มีข ้อ กำา หนด
 ดัง นี้
  • จำา นวนและชนิด ข้อ มูล ของ   argument จะต้อ งเหมือ น
    เดิม
  • ชนิด ข้อ มูล ของค่า ที่ส ่ง กลับ จะต้อ งเหมือ นเดิม
  • access modifier จะต้อ งไม่ม ีร ะดับ ตำ่า กว่า เดิม อาทิเ ช่น
    ถ้า เมธอดเดิม เป็น public จะไม่ส ามารถเปลี่ย นเป็น
    private ได้
Dynamic Binding
 ข้อ ดีข องการมีไ ด้ห ลายรูป แบบอีก ประการหนึง
                                              ่    คือ
  การทีส ามารถกำา หนดออปเจ็ค ได้ห ลายรูป แบบ
        ่
  ตัว อย่า งเช่น จากรูป แบบ
          SuperClass obj;

 เราสามารถทีจ ะสร้า งออปเจ็ค ทีช อ obj
             ่                  ่ ื่      ซึ่ง อาจเป็น
  ออปเจ็ค ของคลาสทีเ ป็น superclass หรือ ทีเ ป็น
                   ่                              ่
  subclass ได้ โดยใช้ค ำา สัง ทีม ร ูป แบบดัง นี้
                            ่ ่ ี
          obj = new SuperClass();
   หรือ   obj = new SubClass();

 ตัว อย่า งเช่น
          Student s;
          s = new GradStudent();
รูป แสดงการมีไ ด้ห ลายรูป แบบ
 ตัว อย่า ง
     Student s1 = new Student();
     Student s1 = new GradStudent();
     Student s1 = new PhDStudent();
เมธอดแบบ Overloaded
 ภาษาจาวาอนุญ าตให้ค ลาสใดๆมีเ มธอดทีม ช อ
                                      ่ ี ื่
 เดีย วกัน มากกว่า หนึง เมธอดได้ แต่เ มธอดเหล่า นั้น
                       ่
 จะต้อ งมีจ ำา นวนหรือ ชนิด ข้อ มูล ของ argument ที่
 ต่า งกัน
 ตัว อย่า งต่อ ไปนี้เ ป็น การเขีย นเมธอดแบบ
 overloaded ทีถ ูก ต้อ ง
              ่
    public void setDetails(String ID, String n) {
    }
    public void setDetails(String ID, double GPA) {
    }
    public double setDetails(double GPA, String n) {
    }

 ตัว อย่า งต่อ ไปนี้เ ป็น การเขีย นเมธอดแบบ
 overloaded ทีไ ม่ถ ก ต้อ ง
              ่     ู
ตัว อย่า งโปรแกรม
public class StudentV2 {
 public class StudentV2 {
        private String id;
         private String id;
        private String name;
         private String name;
        private double gpa;
         private double gpa;
        public void setDetails(String ID,String n) {
         public void setDetails(String ID,String n) {
           id = ID;
            id = ID;
           name = n;
            name = n;
        }}
        public void setDetails(String ID,String n,double GPA) {
         public void setDetails(String ID,String n,double GPA) {
           id = ID;
            id = ID;
           name = n;
            name = n;
           gpa = GPA;
            gpa = GPA;
        }}
}}
เมธอดแบบ overridden
 การกำา หนดเมธอดแบบ     overridden เป็น หลัก การ
 ทีส บ เนือ งมาจากหลัก การของการสืบ ทอด โดย
   ่ ื    ่
 คลาสทีเ ป็น subclass สามารถทีจ ะเขีย นเมธอด
            ่                       ่
 ของ superclass ขึ้น ใหม่ไ ด้ วิธ ีก ารนีเ รีย กว่า
                                         ้
 เมธอดใน subclass เป็น เมธอดแบบ overridden
 การเขีย นเมธอดแบบ         overridden มีข ้อ กำา หนด
 ดัง นี้
  • จำา นวนและชนิด ข้อ มูล ของ   argument จะต้อ งเหมือ น
    เดิม
  • ชนิด ข้อ มูล ของค่า ที่ส ่ง กลับ จะต้อ งเหมือ นเดิม
  • access modifier จะต้อ งไม่ม ีร ะดับ ตำ่า กว่า เดิม อาทิเ ช่น
    ถ้า เมธอดเดิม เป็น public จะไม่ส ามารถเปลี่ย นเป็น
    private ได้
ตัว อย่า ง โปรแกรมแสดงเมธอดแบบ
             overridden
class Student {
 class Student {
        ::
        public void showDetails() {
         public void showDetails() {
                System.out.println("ID: "+id);
                 System.out.println("ID: "+id);
                System.out.println("Name: "+name);
                 System.out.println("Name: "+name);
                System.out.println("GPA: "+gpa);
                 System.out.println("GPA: "+gpa);
        }}
}}

public class GradStudentV2 extends Student {
 public class GradStudentV2 extends Student {
        ::
        public void showDetails() {
         public void showDetails() {
                super.showDetails();
                 super.showDetails();
                System.out.println("ThesisTitle: "+thesisTitle);
                 System.out.println("ThesisTitle: "+thesisTitle);
                System.out.println("Supervisor: "+supervisor);
                 System.out.println("Supervisor: "+supervisor);
        }}
}}
Virtual Method Invocation
 โปรแกรมภาษาจาวาพิจ ารณาเรีย กใช้เ มธอดจาก
 ชนิด ของออปเจ็ค ทีส ร้า งขึ้น ตัว อย่า งเช่น คำา สัง
                   ่                                ่
         Student s1 = new GradStudent();
         s1.showDetails();

เป็น คำา สั่ง ทีเ รีย กใช้เ มธอด showDetails() ของคลาส
                ่
 GradStudent ไม่ใ ช่เ มธอดของคลาส Student
 แต่ค อมไพเลอร์ข องภาษาจาวาจะไม่อ นุญ าตให้
 เรีย กใช้เ มธอดใดๆก็ต ามทีไ ม่ม ก ารประกาศอยูใ น
                            ่    ี            ่
 เมธอดของ superclass ทีก ำา หนดไว้ ตัว อย่า งเช่น
                          ่
         Student s1 = new GradStudent();
         s1.getSupervisor();        // Fail to compile
การส่ง ผ่า น argument ได้ห ลาย
                 รูป แบบ
 ในกรณีท เ มธอดมี
          ี่        argument เป็น ข้อ มูล ชนิด
    คลาส เราสามารถทีจ ะส่ง ออปเจ็ค ของคลาสที่
                     ่
    เป็น subclass ของคลาสนัน แทนได้ ตัว อย่า งเช่น
                           ้
    คำา สั่ง
            Student s1 = new Student();
            PhDStudent s2 = new GradStudent();

 เราสามารถทีจ ะเรีย กใช้เ มธอด printInfo(Student
             ่
    s)ได้ห ลายรูป แบบดัง นี้
            printInfo(s1)
            printInfo(s2)
      หรือ printInfo(new   FullTimeStudent())
ตัว ดำา เนิน การ        instanceof

 คีย เ วิร ์ด instanceof
      ์              เป็น ตัว ดำา เนิน การทีใ ช้ก ับ ออป
                                            ่
  เจ็ค และคลาส เพือ ตรวจสอบว่า เป็น ออปเจ็ค ของ
                   ่
  คลาสนัน หรือ ไม่ โดย จะให้ผ ลลัพ ธ์เ ป็น ข้อ มูล ชนิด
          ้
  boolean
 ถ้า ได้ผลลัพ ธ์เ ป็น true
                        แสดงว่า ออปเจ็ค เป็น ของ
  คลาสนัน หรือ เป็น ของคลาสที่ค ลาสนัน สืบ ทอดมา
        ้                            ้
 ตัว อย่า งเช่น
      GradStudent s1 = new GradStudent();
     (s1 instanceof GradStudent) →     true
     (s1 instanceof Student)           →     true
     (s1 instanceof Object)       →    true
     (s1 instanceof String)       →    false
ตัว อย่า ง เมธอดที่แ สดงการใช้
                   instanceof
public void printInfo(Student s) {
 public void printInfo(Student s) {
     if (s instanceof PhDStudent) {
      if (s instanceof PhDStudent) {
        System.out.println("PhD Student");
         System.out.println("PhD Student");
     } else if (s instanceof GradStudent) {
      } else if (s instanceof GradStudent) {
        System.out.println("Graduate Student");
         System.out.println("Graduate Student");
     } else if (s instanceof FullTimeStudent) {
      } else if (s instanceof FullTimeStudent) {
        System.out.println("Full-Time Student");
         System.out.println("Full-Time Student");
     } else if (s instanceof PartTimeStudent) {
      } else if (s instanceof PartTimeStudent) {
        System.out.println("Part-Time Student");
         System.out.println("Part-Time Student");
     } else if (s instanceof Student) {
      } else if (s instanceof Student) {
        System.out.println("Student");
         System.out.println("Student");
     }}
การ Casting ออปเจ็ค
 การ Casting ออปเจ็ค จะทำา ให้ต ัว แปร ของ
 subclass สามารถทีจ ะอ้า งอิง ถึง ออปเจ็ค ของ
                     ่
 subclass ผ่า นตัว แปรของ superclass ได้ อาทิ
 เช่น
    Student s1 = new GradStudent();
    GradStudent s2 = (GradStudent)s1;


 ภาษาจาวาจะตรวจสอบชนิด ของออปเจ็ค ในช่ว ง
 ของ การรัน โปรแกรม ดัง นัน ตัว แปร ของ subclass
                             ้
 จะไม่ส ามารถอ้า งอิง ถึง ออปเจ็ค ของ superclass
 ได้ แม้ว ่า จะทำา การ casting ออปเจ็ค แล้ว ก็ต าม
 อาทิเ ช่น
    Student s1 = new Student();
    GradStudent s2 = (GradStudent)s1;
การเรีย กใช้เ มธอดของออปเจ็ค
 การเรีย กใช้เ มธอดของออปเจ็ค ทีท ำา การ
                                 ่           casting
 ภาษาจาวาจะตรวจสอบชนิด ของออปเจ็ค ในช่ว ง
 ของการรัน โปรแกรม ว่า ออปเจ็ค ดัง กล่า วเป็น
 ออปเจ็ค ของคลาสใด และคลาสนั้น มีเ มธอดทีเ รีย ก่
 ใช้ห รือ ไม่ หากไม่พ บก็จ ะเกิด ข้อ ผิด พลาดในช่ว ง
 ของการรัน โปรแกรม ตัว อย่า งเช่น
    Student s1 = new Student();
    ((GradStudent) s1).getSupervisor();

    // คอมไพล์ผ ่า นได้ แต่จ ะเกิด ข้อ ผิด พลาดในช่ว งของ
 การรัน โปรแกรม
ตัว อย่า งโปรแกรมแสดงการเรีย ก
            ใช้เ มธอด
 public class Student {
  public class Student {
        private String id;
         private String id;
        private String name;
         private String name;
        private double gpa;
         private double gpa;

       public String getID() {
        public String getID() {
          return id;
           return id;
       }}
       public String getName() {
        public String getName() {
          return name;
           return name;
       }}
       public double getGPA() {
        public double getGPA() {
          return gpa;
           return gpa;
       }}
 }}
ตัว อย่า งโปรแกรมแสดงการเรีย ก
            ใช้เ มธอด
 public class GradStudent extends Student {
  public class GradStudent extends Student {
        private String thesisTitle;
         private String thesisTitle;
        private String supervisor;
         private String supervisor;
        public String getThesisTitle() {
         public String getThesisTitle() {
           return thesisTitle;
            return thesisTitle;
        }}
        public String getSupervisor() {
         public String getSupervisor() {
           return supervisor;
            return supervisor;
        }}
 }}
ตัว อย่า งโปรแกรมแสดงการเรีย ก
            ใช้เ มธอด
 public class TestCallingMethods {
  public class TestCallingMethods {
        public static void main(String args[]) {
         public static void main(String args[]) {
                Student s1 = new GradStudent();
                 Student s1 = new GradStudent();
                s1.getSupervisor();
                 s1.getSupervisor();
                // compile error
                 // compile error

               Student s2 = new Student();
                Student s2 = new Student();
               ((GradStudent) s2).getSupervisor();
                ((GradStudent) s2).getSupervisor();
               // runtime error
                // runtime error
       }}
 }}
Constructor
 constructor      เป็น เมธอดทีม ช ื่อ เดีย วกับ ชื่อ คลาส
                               ่ ี
 ซึ่ง มีร ูป แบบดัง นี้
    [modifier] ClassName([arguments]) {
          [statements]
    }
 constructor     เป็น เมธอดทีไ ม่ม ค ่า ทีจ ะส่ง กลับ แต่
                                 ่  ี      ่
 ไม่ต ้อ งระบุค ีย เ วิร ์ด void
                   ์
 โดยทัว ไปคลาสทุก คลาสจะมี
       ่                         default
 constructor ซึ่ง มีร ูป แบบดัง นี้
     public ClassName() {
    }

 เราสาม ารถทีจ ะเขีย น
              ่             constructor ในคลาสใดๆ
ตัว อย่า งโปรแกรมแสดงการเรีย ก
          ใช้ constructor
public class Student {
 public class Student {
        private String id;
         private String id;
        private String name;
         private String name;
        public Student(String ID,String n) {
         public Student(String ID,String n) {
                id = ID;
                 id = ID;
                name = n;
                 name = n;
        }}
}}
public class TestConstructor {
 public class TestConstructor {
        public static void main(String args[]) {
         public static void main(String args[]) {
                Student s = new Student();
                 Student s = new Student();     //illegal
                                                 //illegal
                Student s = new Student("1122","Somchai");
                 Student s = new Student("1122","Somchai");
        }}
   }}
คำา สั่ง     new()

 มีข ั้น ตอนการทำา งานดัง นี้
   • กำา หนดเนื้อ ที่ใ นหน่ว ยความจำา ให้ก ับ ออปเจ็ค
   • กำา หนดค่า เริ่ม ต้น ให้ก ับ คุณ ลัก ษณะของออปเจ็ค
   • กำา หนดค่า ของคุณ ลัก ษณะของออปเจ็ค ตามคำา สั่ง
     กำา หนดค่า ที่ป ระกาศไว้
   • เรีย กใช้ constructor

 ตัว อย่า งเช่น
     public class MyDate {
          private int day = 1;
          private int month = 1;
          private int year = 2000;
          public MyDate(int d,int m,int y) {
                day = d;
                month = m;
                year = y;
          }
     }
รูป แสดงตัว อย่า งการทำา งานขั้น
            ตอนคำา สั่ง new
 คำา สั่ง
     MyDate d1 = new MyDate(16,8,1972);




             ขั้น ตอนที่ห นึ่ง            ขั้น ตอนที่ส อง




             ขั้น ตอนที่ส าม              ขั้น ตอนที่ส ี่
Constructor แบบ Overloaded
 เราสามารถทีจ ะสร้า ง
             ่     constructor แบบ
  overloaded ของคลาสได้
 constructor      แบบ overloaded จะมีจ ำา นวนหรือ
  ชนิด ข้อ มูล ทีแ ตกต่า งกัน ของ arguments
                 ่
 คำา สั่ง new
            ทีใ ช้ใ นการสร้า งออปเจ็ค จะเรีย กใช้
              ่
  constructor ทีม ี argument ทีส ง ผ่า นมาสอด
                   ่             ่ ่
  คล้อ งกับ constructor ทีก ำา หนด
                            ่
 ข้อ ดีข องการสร้า ง   constructor แบบ
  overloaded คือ ทำา ให้เ ราสามารถทีส ร้า งออปเจ็ค
                                      ่
  เริ่ม ต้น ได้ห ลายรูป แบบ
 เมธอดทีช ื่อ this()
         ่              เป็น การเรีย กใช้ constructor
ตัว อย่า งโปรแกรมแสดง
constructor แบบ overloaded
public class Student {
 public class Student {
        private String id;
         private String id;
        private String name;
         private String name;
        private double gpa;
         private double gpa;
        public Student(String ID,String n) {
         public Student(String ID,String n) {
                id = ID;
                 id = ID;
                name = n;
                 name = n;
        }}
        public Student(String ID,String n,double GPA) {
         public Student(String ID,String n,double GPA) {
                id = ID;
                 id = ID;
                name = n;
                 name = n;
                gpa = GPA;
                 gpa = GPA;
        }}
}}
เมธอด this()
 เมธอดทีช ื่อ this()
         ่       เป็น การเรีย กใช้ constructor
 ของคลาสตัว เอง โดยจะต้อ งเป็น คำา สัง แรกสุด ทีอ ยู่
                                     ่          ่
 ใน constructor แบบ overloaded
ตัว อย่า งโปรแกรมแสดงการใช้
          เมธอด this()
 public class Student {
  public class Student {
        private String id;
         private String id;
        private String name;
         private String name;
        private double gpa;
         private double gpa;
        public Student(String ID,String n) {
         public Student(String ID,String n) {
                id = ID;
                 id = ID;
                name = n;
                 name = n;
        }}
        public Student(String ID,String n,double GPA) {
         public Student(String ID,String n,double GPA) {
                this(ID,n);
                 this(ID,n);
                gpa = GPA;
                 gpa = GPA;
        }}
}}
เมธอด       super()

 constructorของ superclass จะไม่ส บ ทอด
                                   ื
 มายัง subclass
 เราสามารถทีจ ะเรีย กใช้
             ่             constructor ของ
 superclass ได้โ ดยใช้เ มธอดทีช ื่อ
                                ่
  super() โดยส่ง ผ่า น argument ทีส อดคล้อ งกัน
                                   ่
 เมธอด super()
             จะต้อ งเป็น คำา สัง แรกของ
                               ่
 constructor เช่น เดีย วกับ เมธอดทีช อ่ ื่   this()
 โดยทัว ไปการทำา งานของ
       ่                         constructor ของ
 คลาสใดๆจะมีผ ลทำา ให้ม ก ารเรีย กใช้
                               ี
 constructor ของ superclass นัน ซึ่ง ถ้า ไม่ม ค ำา
                                       ้         ี
 สั่ง super() อยูใ นคำา สั่ง แรกของ constructor ของ
                 ่
ตัว อย่า ง คลาสทีม ี constructor ใน
                  ่
             รูป แบบต่า งๆ
 public class Student {
  public class Student {
         protected String name;
          protected String name;
         public Student(String n) {
          public Student(String n) {
            name = n;
             name = n;
         }}
 }}
 public class GradStudent extends Student {
  public class GradStudent extends Student {
         public GradStudent(String n) {
          public GradStudent(String n) {
            super(n);
             super(n);
         }}
         public GradStudent() {
          public GradStudent() {
            this(" ");
             this(" ");
         }}
 }}
ขั้น ตอนการทำา งานของ
                 constructor
1.   ถ้า มีค ำา สั่ง this() ใน constructor ก็จ ะเรีย กใช้
     constructor แบบ overloaded ทีส อดคล้อ งกับ
                                            ่
     คำา สัง this() แล้ว ข้า มไปขั้น ตอนที่ 4
           ่
2.   เรีย กใช้ constructor ของ superclass ถ้า ไม่ม ี
     คำา สัง super() จะเรีย กใช้ constructor แบบ
           ่
     default ยกเว้น คลาสทีช อ Object จะไม่ม ก าร
                               ่ ื่             ี
     เรีย กใช้ constructor ของ superclass
     เนื่อ งจากคลาสทีช ื่อ Object จะไม่ม ี superclass
                        ่
3.   เรีย กใช้ค ำา สั่ง กำา หนดค่า เริ่ม ต้น ของคุณ ลัก ษณะ
     ของออปเจ็ค
4.   เรีย กใช้ค ำา สั่ง ภายใน constructor ของคลาสที่
เมธอดของคลาสที่ช ื่อ                       Object

 คลาสทีช ื่อ Object
        ่                 จะเป็น คลาสทีท ก ๆคลาสจะ
                                       ่ ุ
 สืบ ทอดมา
 เมธอดของคลาส Object             ทีส ำา คัญ คือ
                                    ่
   •   toString()
       เป็น เมธอดที่ใ ช้ใ นการแปลงค่า ของออปเจ็ค ให้เ ป็น
       String ซึ่ง จะส่ง ค่า กลับ เป็น ข้อ มูล ชนิด คลาส String
   •   equals(Object o)
       เป็น เมธอดที่ใ ช้ใ นการเปรีย บเทีย บค่า ของออปเจ็ค ซึ่ง
       จะส่ง ค่า กลับ เป็น ข้อ มูล ชนิด boolean
เมธอด     toString()

 คลาสทีต ้อ งการจะแปลงข้อ มูล เป็น String
        ่                                    จะต้อ งมี
  เมธอดแบบ overriden ทีช อ toString()
                        ่ ื่
 เช่น คลาส Date ได้ก ำา หนดคำา สั่ง สำา หรับ เมธอด
  toString() ไว้แ ล้ว ดัง นัน การเรีย กใช้เ มธอด
                            ้
  System.out.println()โดยที่ argument เป็น ออป
  เจ็ค ของคลาส Date จะทำา ให้ม ก ารเรีย กใช้เ มธอด
                                   ี
  toString() ของคลาส Date โดยอัต โนมัต ิ ตัว อย่า ง
 เช่น คำา สัง
            ่
          Date d = new Date();
          System.out.println(d);
ตัว อย่า ง โปรแกรมแสดงเมธอด
                     toString()
public class Student {
 public class Student {
        private String name;
         private String name;
        public Student(String n) {
         public Student(String n) {
           name = n;
            name = n;
        }}
        public String toString() {
         public String toString() {
           return name;
            return name;
        }}
}}
public class TestToString {
 public class TestToString {
        public static void main(String args[]) {
         public static void main(String args[]) {
           Student s1 = new Student("Thana");
            Student s1 = new Student("Thana");
           System.out.println(s1);
            System.out.println(s1);
        }}
}}



     ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม
               ี่                 Thana
เมธอด       equals()

 คลาสทีต ้อ งการสร้า งเมธอดเพือ เปรีย บเทีย บ ค่า ขอ
        ่                      ่
 ง ออปเจ็ค ว่า เท่า กัน หรือ ไม่ จะต้อ ง มีเ มธอดแบบ
 overriden ทีช อ equals()
                ่ ื่
ตัว อย่า ง โปรแกรมแสดงเมธอด
                    equals()
class Student {
  class Student {
      private String name;
       private String name;
      public Student(String n) {
       public Student(String n) {
         name = n;
          name = n;
      }}
      public boolean equals(Object obj) {
       public boolean equals(Object obj) {
         if (obj.equals(name)) { return true;}
          if (obj.equals(name)) { return true;}
         else return false;
          else return false;
      }}
}}
public class TestEquals {
 public class TestEquals {
      public static void main(String args[]) {
       public static void main(String args[]) {
         Student s1 = new Student("Thana");
          Student s1 = new Student("Thana");
         Student s2 = new Student("Thana");
          Student s2 = new Student("Thana");
         System.out.println(s1.equals(s2));
          System.out.println(s1.equals(s2));
      }}
}}

 ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม
           ี่                 true
คลาสประเภท Wrapper
 คลาสประเภท       Wrapper จะช่ว ยในการสร้า งออป
 เจ็ค ทีเ ก็บ ชนิด ข้อ มูล แบบพืน ฐานไว้ใ นคอลเล็ก ชั่น
        ่                       ้
 แบบ Heterogeneous
 คลาสประเภท
               ชนิด ข้อ มูล มีด ัง นี้
                Wrapper          คลาส
                 boolean       Boolean
                   byte          Byte
                  short         Short
                   int         Integer
                   long          Long
                  float         Float
                 double        Double
                   char       Character
ตัว อย่า งโปรแกรมแสดงการใช้
    คลาสประเภท Wrapper
public class ShowWrapper {
 public class ShowWrapper {
    public static void main(String args[]) {
     public static void main(String args[]) {
          Boolean b = new Boolean(true);
           Boolean b = new Boolean(true);
          Character c = new Character('A');
           Character c = new Character('A');
          Double d = new Double(1.234);
           Double d = new Double(1.234);
          System.out.println(b);
           System.out.println(b);
          System.out.println(c);
           System.out.println(c);
          System.out.println(d);
           System.out.println(d);
     }}
}}

                                   true
     ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม
               ี่                  A
                                   1.234
คุณ ลัก ษณะแบบ          static

 คีย เ วิร ์ด static
      ์      สามารถทีจ ะใช้ก ับ คุณ ลัก ษณะและ
                     ่
  เมธอดของคลาสได้
 คุณ ลัก ษณะของคลาสทีเ ป็น แบบ static
                      ่                 จะเป็น
  คุณ ลัก ษณะทีท ก ออปเจ็ค ของคลาสนี้ใ ช้ร ่ว มกัน
               ่ ุ
 คุณ ลัก ษณะของคลาสทีเ ป็น แบบ static
                      ่                   สามารถ
  ถูก เรีย กใช้จ ากชื่อ ของคลาสได้โ ดยไม่จ ำา เป็น ต้อ ง
  สร้า งออปเจ็ค อาทิเ ช่น
   •   Student.counter
ตัว อย่า งโปรแกรมแสดงการใช้
          คีย ์เ วิร ์ด static
 public class Student {
  public class Student {
       static int counter;
        static int counter;
       public Student() {
        public Student() {
          counter++;
           counter++;
       }}
  }}
  public class TestStatic {
   public class TestStatic {
       public static void main(String args[]) {
        public static void main(String args[]) {
          Student s1 = new Student();
           Student s1 = new Student();
          Student s2 = new Student();
           Student s2 = new Student();
          System.out.println(Student.counter);
           System.out.println(Student.counter);
          System.out.println(s1.counter);
           System.out.println(s1.counter);
          System.out.println(s2.counter);
           System.out.println(s2.counter);
       }}
  }}

                               2
 ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม
           ี่                  2
                               2
เมธอดแบบ              static

 เมธอดโดยทัว ไปจะ มี
            ่             modifier เป็น แบบ non-
  static
 เมธอดทีม ี
         ่    modifier เป็น แบบ static จะสามารถ
  ถูก เรีย กใช้ง าน โดยใช้ช อ คลาสได้เ ลยไม่จ ำา เป็น
                            ื่
  ต้อ งสร้า งออปเจ็ก ของคลาสนั้น ขึ้น มาก่อ น ซึ่ง มีร ูป
  แบบดัง นี้
           className.methodName();
 ตัว อย่า งเช่น
              เมธอดทุก เมธอดในคลาส Math เป็น
  แบบ static ดัง นัน การเรีย กใช้ง านทุก เมธอดใน
                   ้
  คลาสสามารถทำา ได้ เช่น
  Math.sqrt(4);

 เมธอดแบบ static      จะไม่ส ามารถเรีย กใช้เ มธอด
Static Initializer
 StaticInitializer คือ บล็อ กในคลาสใดๆทีอ ยู่ ่
 นอกเมธอด และมีค ีย เ วิร ์ด static เพือ นิย ามให้เ ป็น
                     ์                 ่
 บล็อ กแบบ static
 รูป แบบของ    Static Initializer
    static {
       ...
    }

 คำา สั่ง ในบล็อ กแบบ static
                       จะถูก เรีย กใช้เ พีย งครั้ง
 เดีย วเมือ JVM โหลดคลาสดัง กล่า วขึ้น มา
          ่
      Initializer ใช้ใ นการ ดีบ ัก (debug)
 Static
 โปรแกรม หรือ ใช้ใ นกรณีท ต ้อ งการสร้า งออปเจ็ค
                            ี่
 ของคลาสขึ้น โดยอัต โนมัต ิ
ตัว อย่า งโปรแกรมแสดง Static
           Initializer
   public class TestStaticBlock {
    public class TestStaticBlock {
        static int x=5;
         static int x=5;
        static {
         static {
            x += 1;
             x += 1;
        }}
        public static void main(String args[]) {
         public static void main(String args[]) {
            System.out.println("x = "+x);
             System.out.println("x = "+x);
        }}
        static {
         static {
            x /= 2;
             x /= 2;
        }}
   }}



ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม 3
          ี่                  x =
คีย ์เ วิร ์ด   final

 คีย เ วิร ์ด final
      ์                สามารถจะใช้ไ ด้ก บ คลาส ตัว แปร
                                        ั
  และเมธอด
 คลาสทีม ี
        ่  modifier เป็น final จะทำา ให้ค ลาสอื่น
  ไม่ส ามารถสืบ ทอดคลาสนี้ไ ด้
 เมธอดทีม ี
         ่  modifier เป็น final คือ เมธอดที่จ ะไม่
  สามารถมีเ มธอดแบบ overriden ได้
 ตัว แปรทีม ี
           ่  modifier เป็น final คือ ค่า คงที่ ซึ่ง จะ
  ทำา ให้ส ามารถกำา หนดค่า ได้เ พีย งครั้ง เดีย วเท่า นัน
                                                        ้
คลาสแบบ          abstract

 คลาสทีม ี
        ่    modifier เป็น abstract หมายความว่า
  คลาสนัน ยัง เป็น คลาสทีไ ม่ส มบูร ณ์ โดยมีเ มธอด
         ้                 ่
  แบบ abstract ซึ่ง เป็น เมธอดทีย ัง ไม่ส มบูร ณ์อ ย่า ง
                                ่
  น้อ ยหนึง เมธอดอยู่ใ นคลาส
           ่
 รูป แบบของเมธอดแบบ abstract

  [modifier] abstract return_type methodName([arguments]);

 คลาสแบบ abstract     กำา หนดขึ้น มาเพือ ให้ค ลาสอื่น
                                         ่
  สืบ ทอด โดยคลาสทีม าสืบ ทอดจะต้อ งกำา หนด
                       ่
  บล็อ กคำา สั่ง ในเมธอดทีย ง ไม่ส มบูร ณ์
                           ่ ั
 เราไม่ส ามารถสร้า งออปเจ็ค ของคลาสแบบ
  abstract   ได้
ตัว อย่า งโปรแกรมแสดงคลาสแบบ
                       abstract
 public abstract class Student {
  public abstract class Student {
         protected String id;
          protected String id;
         protected String name;
          protected String name;
         protected double gpa;
          protected double gpa;
         public void setID(String ID) {
          public void setID(String ID) {
                 id = ID;
                  id = ID;
         }}
         public void setName(String n) {
          public void setName(String n) {
                 name = n;
                  name = n;
         }}
         public void setGPA(double GPA) {
          public void setGPA(double GPA) {
                 gpa = GPA;
                  gpa = GPA;
         }}
         public abstract void showDetails();
          public abstract void showDetails();
 }}
ตัว อย่า งคลาสที่ส บ ทอดมาจาก
                    ื
        คลาสแบบ abstract
public class FullTimeStudent extends Student {
 public class FullTimeStudent extends Student {
        private int credit;
         private int credit;
        private final int MAX_YEAR = 4;
         private final int MAX_YEAR = 4;
        public FullTimeStudent(int c) {
         public FullTimeStudent(int c) {
                credit = c;
                 credit = c;
        }}
        public void showDetails() {
         public void showDetails() {
                System.out.println("ID: "+id);
                 System.out.println("ID: "+id);
                System.out.println("Name: "+name);
                 System.out.println("Name: "+name);
                System.out.println("GPA: "+gpa);
                 System.out.println("GPA: "+gpa);
                System.out.println("Credit: "+credit);
                 System.out.println("Credit: "+credit);
        }}
}}
อิน เตอร์เ ฟส
 อิน เตอร์เ ฟส (interface) มีล ัก ษณะคล้า ยกับ คลาส
 แบบ abstract แต่จ ะประกอบด้ว ยเมธอดทีย ง ไม่
                                            ่ ั
 สมบูร ณ์เ ท่า นัน
                 ้
 รูป แบบของอิน เตอร์เ ฟส
     [modifier] interface InterfaceName {
          [methods();]
     }

 อิน เตอร์เ ฟสกำา หนดขึ้น มาเพือ ให้ค ลาสอื่น นำา ไป
                                ่
 ใช้ง านโดยใช้ค ีย เ วิร ์ด
                   ์          implements   โดยมีร ูป แบบ
 ดัง นี้
     [modifier] class ClassName implements InterfaceName {
          [methods();]
อิน เตอร์เ ฟส
 อิน เตอร์เ ฟสจะเหมือ นกับ คลาสแบบ abstractตรงที่
 เราจะไม่ส ามารถสร้า งออปเจ็ค ของอิน เตอร์เ ฟสได้
 ประโยชน์ข องอิน เตอร์เ ฟสคือ
  • การกำา หนดรูป แบบของเมธอดต่า งๆที่ค ลาสอื่น ๆจะต้อ ง
    implements     ไว้ล ่ว งหน้า ซึ่ง สามารถอาศัย หลัก การของ
    การมีไ ด้ห ลายรูป แบบมาเรีย กใช้เ มธอดเหล่า นั้น ได้จ าก
    คลาสที่ implements อิน เตอร์เ ฟส
  • ภาษาจาวากำา หนดให้ค ลาสใดๆสามารถสืบ ทอดคลาส
    อื่น ได้เ พีย งคลาสเดีย วเท่า นั้น แต่จ ะสามารถ implements
    อิน เตอร์เ ฟสได้ห ลายอิน เตอร์เ ฟส
ตัว อย่า งอิน เตอร์เ ฟส
     public interface Student {
      public interface Student {
          public void setID(String ID);
           public void setID(String ID);
          public void setName(String n);
           public void setName(String n);
          public void setGPA(double GPA);
           public void setGPA(double GPA);
          public void showDetails();
           public void showDetails();
}}
ตัว อย่า ง คลาสที่ implements
         อิน เตอร์เ ฟส
public class PartTimeStudent implements Student {
 public class PartTimeStudent implements Student {
     private String id;
      private String id;
     private String name;
      private String name;
     private double gpa;
      private double gpa;
     private int credit;
      private int credit;
     private final int MAX_YEAR = 8;
      private final int MAX_YEAR = 8;
     public PartTimeStudent(int c) {
      public PartTimeStudent(int c) {
             credit = c;
              credit = c;
     }}
     public void setID(String ID) {
      public void setID(String ID) {
             id = ID;
              id = ID;
     }}
     public void setName(String n) {
      public void setName(String n) {
             name = n;
              name = n;
     }}
ตัว อย่า ง คลาสที่ implements
             อิน เตอร์เ ฟส
     public void setGPA(double GPA) {
      public void setGPA(double GPA) {
             gpa = GPA;
              gpa = GPA;
     }}
     public void showDetails() {
      public void showDetails() {
             System.out.println("ID: "+id);
              System.out.println("ID: "+id);
             System.out.println("Name: "+name);
              System.out.println("Name: "+name);
             System.out.println("GPA: "+gpa);
              System.out.println("GPA: "+gpa);
             System.out.println("Credit: "+credit);
              System.out.println("Credit: "+credit);
     }}
}}
คลาสภายใน
 คลาสภายใน  (Inner class) คือ คลาสทีป ระกาศ
                                           ่
 อยูภ ายในคลาสอื่น ๆ ซึง บางครั้ง เรีย กว่า คลาส
    ่                  ่
 แบบซ้อ น (nested class)
 คลาสภายในอนุญ าตให้ป ระกาศคุณ ลัก ษณะหรือ
 เมธอดภายในคลาสอื่น ได้
 คลาสภายในมีป ระโยชน์ใ นกรณีท ต ้อ งการจะจัด
                               ี่
 กลุ่ม ของคลาสทีต ้อ งทำา งานร่ว มกัน โดยต้อ งการ
                   ่
 ควบคุม ไม่ใ ห้ม ก ารเข้า ถึง โดยตรงจากคลาสอื่น ๆ
                 ี
 และต้อ งการเรีย กใช้ค ุณ ลัก ษณะหรือ เมธอดของ
 คลาสทีอ ยู่ภ ายนอกได้โ ดยตรง
         ่
 คลาสภายในทีใ ช้ใ นภาษาจาวาแบ่ง ออกเป็น
             ่
  • คลาสภายในที่อ ยู่ภ ายในคลาสโดยตรง
คลาสภายในทีอ ยู่ภ ายในคลาส
              ่
 กรณีน เ ป็น การประกาศคลาสภายในคลาสอื่น ที่
        ี้
 เรีย กว่า คลาสภายนอก (Outer class)
 คลาสภายในสามารถมี        access modifier เป็น
 public, private,   default หรือ protected ได้
 การสร้า งออปเจ็ค ของคลาสภายในมีข อ แตกต่า ง
                                   ้
 ดัง นี้
  • การสร้า งออปเจ็ค ของคลาสภายในนอกคลาสภายนอก
    จะต้อ งทำา การสร้า งออปเจ็ค ของคลาสภายนอกก่อ น
    แล้ว จึง สร้า งออปเจ็ค ของคลาสภายในได้
  • การสร้า งออปเจ็ค ภายในเมธอดที่อ ยู่ใ นคลาสภายนอก
    สามารถทำา ได้โ ดยตรง
 คลาสทีอ ยู่ภ ายในสามารถที่จ ะเรีย กใช้ค ุณ ลัก ษณะ
        ่
ตัว อย่า งโปรแกรมแสดงคลาสที่อ ยู่
            ภายในคลาส
 public class Outer {
  public class Outer {
        public void method1() {
         public void method1() {
            Inner in1 = new Inner();
             Inner in1 = new Inner();
            in1.method2();
             in1.method2();
        }}
        public class Inner {
         public class Inner {
            public void method2() {
             public void method2() {
                System.out.println("Inner Demo");
                 System.out.println("Inner Demo");
            }}
        }}
 }}
 public class InnerDemo {
  public class InnerDemo {
        public static void main(String args[]) {
         public static void main(String args[]) {
            Outer.Inner in2 = new Outer().new Inner();
             Outer.Inner in2 = new Outer().new Inner();
            in2.method2();
             in2.method2();
        }}
 }}
คลาสภายในที่อ ยู่ภ ายในเมธอด
 คลาสภายในประเภทนี้จ ะมี    access modifier เป็น
 default
 เราจะไม่ส ามารถทีจ ะสร้า งออปเจ็ค ของคลาส
                   ่
 ภายในประเภทนี้น อกเมธอดทีป ระกาศคลาสได้
                          ่
 คลาสประเภทนีจ ะสามารถเรีย กใช้ต ัว แปรภายใน
              ้
 ของเมธอดได้ใ นกรณีท ต ัว แปรนั้น ประกาศเป็น
                              ี่
 final เท่า นัน ส่ว นตัว แปรทีเ ป็น คุณ ลัก ษณะของ
              ้                  ่
 คลาสหรือ คุณ ลัก ษณะของออปเจ็ค สามารถทีจ ะ      ่
 เรีย กใช้ไ ด้เ ช่น เดีย วกับ คลาสภายในทีอ ยูภ ายใน
                                             ่ ่
 คลาส
คุณ สมบัต ิท ส ำา คัญ อื่น ๆของคลาส
               ี่
                  ภายใน
 คลาสภายในอาจเป็น คลาสแบบ abstract        หรือ
 อิน เตอร์เ ฟสได้
 คลาสภายในทีอ ยูภ ายในคลาสภายนอกโดยตรง
             ่ ่
 ถ้า มี modifier เป็น static จะกลายเป็น คลาสปกติ
 และสามารถสร้า งออปเจ็ค โดยการเรีย กชื่อ ของ
 คลาสภายนอกได้ด ัง นี้
         Outer.Inner in3 = Outer.new Inner();
 คลาสภายในไม่ส ามารถทีป ระกาศให้ม ี
                       ่
 คุณ ลัก ษณะหรือ เมธอดเป็น แบบ static ได้เ ว้น แต่
 คลาสภายในจะเป็น คลาสแบบ static
 คลาสภายในสามารถทีจ ะใช้ต ัว แปรทีเ ป็น
                   ่               ่
ตัว อย่า งโปรแกรมแสดงคลาสที่อ ยู่
           ภายในเมธอด
 public class MOuter {
  public class MOuter {
      private int a = 1;
       private int a = 1;
      public void method(final int b) {
       public void method(final int b) {
           final int c = 2;
            final int c = 2;
           int d = 3;
            int d = 3;
           class Inner {
            class Inner {
                private void iMethod() {
                 private void iMethod() {
                    System.out.println("a = "+a);
                     System.out.println("a = "+a);
                    System.out.println("b = "+b);
                     System.out.println("b = "+b);
                    System.out.println("c = "+c);
                     System.out.println("c = "+c);
                    System.out.println("d = "+d);
                     System.out.println("d = "+d);   //illegal
                                                      //illegal
                }}
           }}
      }}
 }}
แพคเก็จ
 ซอฟต์แ วร์แ พคเก็จ ช่ว ยในการจัด การการพัฒ นา
 โปรแกรมขนาดใหญ่
 ในโปรแกรมภาษาจาวา
                  แพคเก็จ จะเป็น ทีร วมของ
                                   ่
 คลาสของภาษาจาวาหลายๆคลาส
 โปรแกรมอาจแบ่ง เป็น แพคเก็จ และแพคเก็จ ย่อ ย
 (Subpackage)
 แพค เก็จ จะเก็บ ไว้ใ นไดเร็ก ทอรี่   (Directory) ซึ่ง
 จะเป็น ชื่อ ของแพคเก็จ
ตัว อย่า ง
โครงสร้า งโปรแกรมภาษาจาวา
 รูป แบบโปรแกรมภาษาจาวามีด ัง นี้
      [<package_declaration>]
      [<import_declaration>]
    [<class_declaration>]
คำา สัง
                           ่   package

 คำา สั่ง package   เป็น การระบุว า คลาสอยูใ นแพคเก็จ
                                   ่        ่
  ใด
 รูป แบบของคำา สั่ง     package
     package <package_name>[<sub_package_name>];

 ตัว อย่า ง
     package faculty.domain;

 โปรแกรมภาษาจาวาหนึ่ง โปรแกรมจะมีค ำา สัง
                                         ่
       ได้เ พีย งคำา สั่ง เดีย ว โดยจะเป็น คำา สั่ง แรก
  package
  ของโปรแกรม
 กรณีท ไ ม่ม ค ำา สั่ง package
        ี่    ี                    คลาสจะถูก กำา หนดไว้ใ น
  แพคเก็จ default
คำา สั่ง      import

 คำา สั่ง import    เป็น การเรีย กใช้ค ลาสในแพคเก็จ
  ต่า งๆ
 รูป แบบของคำา สั่ง       import
   import <package_name>[.<sub_package_name>].<Class_name>
   • หรือ
   import <package_name>[.<sub_package_name>].*;

 ตัว อย่า ง
   import faculty.reports.Report;
   • หรือ
   import java.awt.*;

 คำา สั่ง import    จะอยูก ่อ นหน้า การประกาศคลาส
                          ่
 โปรแกรมภาษาจาวาหนึ่ง โปรแกรมสามารถมีค ำา
  สั่ง   import   ได้ห ลายคำา สั่ง
สรุป เนื้อ หาของบท
 คลาสจะประกอบไปด้ว ยคุณ ลัก ษณะและเมธอด

 การเขีย นโปรแกรมเชิง ออปเจ็ค เป็น การแบ่ง
 โปรแกรมออกเป็น โมดูล ย่อ ยๆ ทำา ให้ส ามารถปรับ
 เปลี่ย นแก้ไ ขโปรแกรมได้ง ่า ยขึ้น
 เมธอดทีก ำา หนดขึ้น ในคลาสใดๆสามารถเรีย กใช้
         ่
 งานได้ส องรูป แบบคือ การเรีย กใช้ง านจากคลาส
 ทีต ่า งกัน และการเรีย กใช้ง านภายในคลาส
   ่
 เดีย วกัน
 เมธอดทีก ำา หนดขึ้น ในคลาสอาจมี
         ่                          argument ที่
 รับ ค่า เพือ นำา ไปใช้ใ นเมธอด
            ่
 เมธอดใดๆของคลาสสามารถทีจ ะมีค ่า ทีส ่ง กลับ มา
                         ่           ่
สรุป เนื้อ หาของบท
 เมธอดของคลาสใดทีม ี
                  ่      modifier เป็น แบบ static
 จะทำา ให้ส ามารถถูก เรีย กใช้ง าน โดยใช้ช ื่อ ของ
 คลาสนัน ได้เ ลย
        ้
 เมธอดแบบ    overloaded คือ มีเ มธอดทีม ช ื่อ่ ี
 เดีย วกัน แต่จ ะมีจ ำา นวนหรือ ชนิด ข้อ มูล ของ
 argument ทีต ่า งกัน
                 ่
 โดยทัว ไป
       ่    คุณ ลัก ษณะของคลาสจะมี modifier
 เป็น แบบ private ทัง นีเ พือ ป้อ งกัน การเข้า ถึง จาก
                     ้ ้ ่
 คลาสอื่น
 โดยทัว ไป
       ่   เมธอดของคลาสจะมี modifier เป็น
 แบบ public ทัง นีเ พือ ให้ เมธอดจากคลาสอื่น เรีย ก
              ้ ้ ่
สรุป เนื้อ หาของบท
 ชื่อ ของเมธอดทีเ กี่ย วข้อ งกับ คุณ ลัก ษณะของคลาส
                 ่
 นิย มใช้ setXxx สำา หรับ เมธอดทีม ไ ว้เ พือ กำา หนด
                                     ่ ี   ่
 ค่า ให้ก ับ คุณ ลัก ษณะ xxx และใช้ getXxx สำา หรับ
 เมธอดทีจ ะส่ง ค่า กลับ เป็น คุณ ลัก ษณะ xxx
            ่
 ภาษาจาวาได้ก ำา หนดคีย เ วิร ์ด ทีช ื่อ extends
                         ์          ่               เพือ
                                                       ่
  ระบุก ารสืบ ทอดของคลาส
 คลาสทุก คลาสในภาษาจาวาถ้า ไม่ไ ด้ส ืบ ทอดจาก
  คลาสใดเลยจะถือ ว่า คลาสนั้น สืบ ทอดจากคลาสที่
  ชื่อ Object
 เมธอดแบบoverriden จะเป็น การเขีย นเมธอด
 ของ superclass ขึ้น ใหม่
สรุป เนื้อ หาของบท
 Constructor        หมายถึง เมธอดทีม ช ื่อ เดีย วกัน กับ
                                    ่ ี
 ชื่อ คลาส แต่จ ะไม่ม ก ารส่ง ค่า กลับ และจะไม่ม ก าร
                         ี                           ี
 ใส่ค ีย เ วิร ์ด void โดยคลาสทุก คลาสจะมี default
         ์
 constructor มาให้อ ยูแ ล้ว แต่เ ราสามารถที่จ ะ
                            ่
 กำา หนด constructor ขึ้น ใหม่เ องได้
 เมธอดทีช ื่อ this()
         ่              เป็น การเรีย กใช้ constructor
 ของคลาสตัว เอง
 คลาสแบบ final     คือ คลาสทีไ ม่ส ามารถสืบ ทอดได้
                              ่
 คลาสแบบ abstract      คือ คลาสทีย ง เป็น คลาสทีไ ม่
                                     ่ ั         ่
 สมบูร ณ์ โดยมีเ มธอดแบบ abstract ซึ่ง เป็น เมธอดที่
 ยัง ไม่ส มบูร ณ์อ ย่า งน้อ ยหนึ่ง เมธอด
แบบฝึก หัด
 แบบฝึก หัด ที่
          1 การเขีย น constructor และ
 เมธอดแบบ overloaded
   • จงเขีย นคำา สั่ง ในเมธอดต่า งๆของคลาส Rectangle    ให้
    สมบูร ณ์
 แบบฝึก หัด ที่   2 การเขีย นโปรแกรมบัญ ชี
   • จงเขีย นคลาสที่ช ื่อ Account
   • จงเขีย นคลาสที่ช ื่อ Teller ซึ่ง จะเป็น คลาสที่ใ ช้ใ นการ
     สร้า งออปเจ็ค ของคลาส Account และทดสอบการเรีย ก
    ใช้เ มธอดต่า งๆ
 แบบฝึก หัด ที่   3 การเขีย นโปรแกรมบัญ ชีเ ช็ค
   • จงเขีย นคลาสที่ช ื่อ CheckingAccount
   • จงแก้ไ ขคลาสที่ช ื่อ TellerGUI โดยใช้ห ลัก การของ
    Dynamic Binding เพื่อ สร้า ง     ออปเจ็ค ของคลาส

More Related Content

Similar to Java Programming [6/12] : Object Oriented Java Programming (8)

PDF
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
Finian Nian
 
PDF
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
Finian Nian
 
PDF
Java-Answer Chapter 12-13
Wongyos Keardsri
 
PDF
Pbl8.2
Sumitrazaza
 
PDF
Java-Answer Chapter 08-09
Wongyos Keardsri
 
PDF
Java-Answer Chapter 12-13 (For Print)
Wongyos Keardsri
 
PDF
Java-Chapter 02 Data Operations and Processing
Wongyos Keardsri
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
Finian Nian
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
Finian Nian
 
Java-Answer Chapter 12-13
Wongyos Keardsri
 
Pbl8.2
Sumitrazaza
 
Java-Answer Chapter 08-09
Wongyos Keardsri
 
Java-Answer Chapter 12-13 (For Print)
Wongyos Keardsri
 
Java-Chapter 02 Data Operations and Processing
Wongyos Keardsri
 

More from IMC Institute (20)

PDF
นิตยสาร Digital Trends ฉบับที่ 14
IMC Institute
 
PDF
Digital trends Vol 4 No. 13 Sep-Dec 2019
IMC Institute
 
PDF
บทความ The evolution of AI
IMC Institute
 
PDF
IT Trends eMagazine Vol 4. No.12
IMC Institute
 
PDF
เพราะเหตุใด Digitization ไม่ตอบโจทย์ Digital Transformation
IMC Institute
 
PDF
IT Trends 2019: Putting Digital Transformation to Work
IMC Institute
 
PDF
มูลค่าตลาดดิจิทัลไทย 3 อุตสาหกรรม
IMC Institute
 
PDF
IT Trends eMagazine Vol 4. No.11
IMC Institute
 
PDF
แนวทางการทำ Digital transformation
IMC Institute
 
PDF
บทความ The New Silicon Valley
IMC Institute
 
PDF
นิตยสาร IT Trends ของ IMC Institute ฉบับที่ 10
IMC Institute
 
PDF
แนวทางการทำ Digital transformation
IMC Institute
 
PDF
The Power of Big Data for a new economy (Sample)
IMC Institute
 
PDF
บทความ Robotics แนวโน้มใหม่สู่บริการเฉพาะทาง
IMC Institute
 
PDF
IT Trends eMagazine Vol 3. No.9
IMC Institute
 
PDF
Thailand software & software market survey 2016
IMC Institute
 
PPTX
Developing Business Blockchain Applications on Hyperledger
IMC Institute
 
PDF
Digital transformation @thanachart.org
IMC Institute
 
PDF
บทความ Big Data จากบล็อก thanachart.org
IMC Institute
 
PDF
กลยุทธ์ 5 ด้านกับการทำ Digital Transformation
IMC Institute
 
นิตยสาร Digital Trends ฉบับที่ 14
IMC Institute
 
Digital trends Vol 4 No. 13 Sep-Dec 2019
IMC Institute
 
บทความ The evolution of AI
IMC Institute
 
IT Trends eMagazine Vol 4. No.12
IMC Institute
 
เพราะเหตุใด Digitization ไม่ตอบโจทย์ Digital Transformation
IMC Institute
 
IT Trends 2019: Putting Digital Transformation to Work
IMC Institute
 
มูลค่าตลาดดิจิทัลไทย 3 อุตสาหกรรม
IMC Institute
 
IT Trends eMagazine Vol 4. No.11
IMC Institute
 
แนวทางการทำ Digital transformation
IMC Institute
 
บทความ The New Silicon Valley
IMC Institute
 
นิตยสาร IT Trends ของ IMC Institute ฉบับที่ 10
IMC Institute
 
แนวทางการทำ Digital transformation
IMC Institute
 
The Power of Big Data for a new economy (Sample)
IMC Institute
 
บทความ Robotics แนวโน้มใหม่สู่บริการเฉพาะทาง
IMC Institute
 
IT Trends eMagazine Vol 3. No.9
IMC Institute
 
Thailand software & software market survey 2016
IMC Institute
 
Developing Business Blockchain Applications on Hyperledger
IMC Institute
 
Digital transformation @thanachart.org
IMC Institute
 
บทความ Big Data จากบล็อก thanachart.org
IMC Institute
 
กลยุทธ์ 5 ด้านกับการทำ Digital Transformation
IMC Institute
 
Ad

Java Programming [6/12] : Object Oriented Java Programming

  • 1. บทที่ 6 การพัฒ นาโปรแกรมภาษา จาวาเชิง ออปเจ็ค (Object Oriented Java Programming) อ.ธนิศ า เครือ ไวศยวรรณ คณะเทคโนโลยีส ารสนเทศ
  • 2. วัต ถุป ระสงค์  อธิบ ายเมธอด และการเรีย กใช้เ มธอด  อธิบ ายคีย เ วิร ์ด ทีใ ช้เ ป็น ์ ่ modifier ของเมธอด  แนะนำา การเขีย นโปรแกรมภาษาจาวาเชิง ออปเจ็ค  อธิบ ายคุณ ลัก ษณะเด่น ของโปรแกรมเชิง ออปเจ็ค  อธิบ ายความหมายของ Constructor
  • 3. การเขีย นโปรแกรมภาษาจาวาเชิง ออปเจ็ค  ตัว อย่า งโปรแกรมในบททีผ ่า น ๆมา ่ บางครั้ง จะ เขีย นทุก คำา สัง อยู่ภ ายใน เมธอดทีช ื่อ main() ซึง ่ ่ ่ ไม่ไ ด้ใ ช้ห ลัก การเชิง ออปเจ็ค และไม่เ หมาะทีจ ะนำา ่ ไปใช้ใ นทางปฏิบ ัต ิเ มือ โปรแกรมมีข นาดใหญ่ข น ่ ึ้  การเขีย นโปรแกรมเชิง ออปเจ็ค ทีด ีจ ะต้อ งมีก าร ่ กำา หนดเมธอดต่า งๆให้ก ับ คลาส
  • 4. ตัว อย่า งโปรแกรมที่ท ุก คำา สัง อยู่ใ น ่ เมธอด main() public class AllInMain { public class AllInMain { public static void main(String args[]) { public static void main(String args[]) { double score = Math.random()*100; double score = Math.random()*100; if (score >= 80) { if (score >= 80) { System.out.println("Grade is A"); System.out.println("Grade is A"); } else if (score >= 70) { } else if (score >= 70) { System.out.println("Grade is B"); System.out.println("Grade is B"); } else if (score >= 60){ } else if (score >= 60){ System.out.println("Grade is C"); System.out.println("Grade is C"); } else if (score >= 50){ } else if (score >= 50){ System.out.println("Grade is D"); System.out.println("Grade is D"); } else { } else { System.out.println("Grade is F"); System.out.println("Grade is F"); }} }} }}
  • 5. ตัว อย่า งโปรแกรมที่ใ ช้ห ลัก การเชิง ออปเจ็ค public class NumericalClass {{ public class NumericalClass public void calculate() {{ public void calculate() double score == Math.random()*100; double score Math.random()*100; if (score >= 80) {{ if (score >= 80) System.out.println("Grade is A"); System.out.println("Grade is A"); }} else if (score >= 70) {{ else if (score >= 70) System.out.println("Grade is B"); System.out.println("Grade is B"); }} else if (score >= 60){ else if (score >= 60){ System.out.println("Grade is C"); System.out.println("Grade is C"); }} else if (score >= 50){ else if (score >= 50){ System.out.println("Grade is D"); System.out.println("Grade is D"); }} else {{ else System.out.println("Grade is F"); System.out.println("Grade is F"); }} }} }} ----------------------------------------------------------------- ----------------------------------------------------------------- ------------------------- ------------------------- public class MyMain { public class MyMain { public static void main(String args[]) {{ public static void main(String args[]) NumericalClass obj == new NumericalClass(); NumericalClass obj new NumericalClass(); obj.calculate(); obj.calculate();
  • 6. ตัว อย่า งโปรแกรมที่ท ุก เมธอดอยู่ ในคลาสเดีย วกัน public class NumericalClassV1 { public class NumericalClassV1 { public void calculate() { public void calculate() { double score = Math.random()*100; double score = Math.random()*100; if (score >= 80) { if (score >= 80) { System.out.println("Grade is A"); System.out.println("Grade is A"); } else if (score >= 70) { } else if (score >= 70) { System.out.println("Grade is B"); System.out.println("Grade is B"); } else if (score >= 60){ } else if (score >= 60){ System.out.println("Grade is C"); System.out.println("Grade is C"); } else if (score >= 50){ } else if (score >= 50){ System.out.println("Grade is D"); System.out.println("Grade is D"); } else { } else { System.out.println("Grade is F"); System.out.println("Grade is F"); }} }} public static void main(String args[]) { public static void main(String args[]) { NumericalClassV1 obj = new NumericalClassV1(); NumericalClassV1 obj = new NumericalClassV1(); obj.calculate(); obj.calculate(); }} }}
  • 7. รูป แบบการเรีย กใช้เ มธอด  รูป แบบของคำา สั่ง ทีม ก ารเรีย กใช้เ มธอดเป็น ดัง นี้ ่ ี obj.methodName([arguments]);  arguments อาจจะเป็น ข้อ มูล ค่า คงทีห รือ ตัว แปร ่  ชนิด ข้อ มูล ของ arguments ที่ใ ช้ใ นการเรีย กเมธอด จะต้อ งสอดคล้อ งกัน กับ ชนิด ข้อ มูล ของ arguments ของเมธอด
  • 8. การเรีย กใช้เ มธอด  เมธอดทีก ำา หนดขึ้น ในคลาสใดๆสามารถเรีย กใช้ ่ งานได้ส องรูป แบบคือ • การเรีย กใช้ง านจากคลาสที่ต ่า งกัน • การเรีย กใช้ง านภายในคลาสเดีย วกัน  การเรีย กใช้เ มธอดจากคลาสทีต ่า งกัน ่ จะต้อ งมี การสร้า งออปเจ็ค ของคลาสทีม เ มธอดทีจ ะถูก เรีย ก ่ ี ่ ใช้ง านก่อ น จึง จะสามารถเรีย กใช้เ มธอดได้  การเรีย กใช้เ มธอดภายในคลาสเดีย วกัน ในบาง กรณีจ ะสามารถทำา ได้โ ดยไม่จ ำา เป็น ต้อ งสร้า งออป เจ็ค ของคลาสขึ้น มาก่อ น และสามารถเรีย กเมธอด ได้ท ก เมธอด ุ
  • 9. ตัว อย่า งโปรแกรมที่ม ีก ารเรีย กใช้ เมธอดในคลาสเดีย วกัน public class NumericalClassV2 { public class NumericalClassV2 { public void calculate() { public void calculate() { double score = Math.random()*100; double score = Math.random()*100; if (score >= 80) { if (score >= 80) { System.out.println("Grade is A"); System.out.println("Grade is A"); } else if (score >= 70) { } else if (score >= 70) { System.out.println("Grade is B"); System.out.println("Grade is B"); } else if (score >= 60){ } else if (score >= 60){ System.out.println("Grade is C"); System.out.println("Grade is C"); } else if (score >= 50){ } else if (score >= 50){ System.out.println("Grade is D"); System.out.println("Grade is D"); } else { } else { System.out.println("Grade is F"); System.out.println("Grade is F"); }} }} public void callMethod() { public void callMethod() { calculate(); calculate(); }} }}
  • 10. การส่ง ผ่า น argument  กรณีท ี่เ มธอด มี argument ทีจ ะรับ ค่า เพือ นำา ไปใช้ใ น ่ ่ เมธอด อาทิเ ช่น public void setGPA(double GPA) { ... }  คำา สั่ง ทีเ รีย กใช้เ มธอดนี้ ่ จะต้อ งส่ง argument ทีม ี ่ ชนิด ข้อ มูล เป็น double ไป พร้อ มกับ ชื่อ เมธอด เช่น setGPA(3.0);
  • 11. ตัว อย่า งโปรแกรม public class Student { public class Student { String id; String id; String name; String name; double gpa; double gpa; public void setGPA(double GPA) { public void setGPA(double GPA) { gpa = GPA; gpa = GPA; }} public double getGPA() { public double getGPA() { return gpa; return gpa; }} }} -------------------------------------------------------------- -------------------------------------------------------------- public class MyMain2 { public class MyMain2 { public static void main(String args[]) { public static void main(String args[]) { Student s1 = new Student(); Student s1 = new Student(); s1.setGPA(3.0); s1.setGPA(3.0); }} }}
  • 12. argument ของเมธอด  argumentของเมธอดจะมีช นิด ข้อ มูล เป็น สอง แบบตามชนิด ข้อ มูล ของ ตัว แปรดัง นี้ • argument ที่ม ีช นิด ข้อ มูล แบบพื้น ฐาน • argument ที่ม ีช นิด ข้อ มูล แบบอ้า งอิง  ในกรณีข อง argument ทีม ช นิด ข้อ มูล แบบพืน ่ ี ้ ฐาน เราสามารถทีจ ะส่ง ค่า คงทีข ้อ มูล ตัว แปร ่ ่ หรือ นิพ จน์ใ ห้ก ับ argument ได้ ตัว อย่า งเช่น • ค่า คงที่ข ้อ มูล เช่น s1.setGPA(3.0); • ตัว แปร เช่น double x = 3.0; s1.setGPA(x); • นิพ จน์ เช่น s1.setGPA(3.0+0.05);
  • 13. argument ที่ม ีช นิด ข้อ มูล แบบ อ้า งอิง  เราจะต้อ งส่ง ออปเจ็ค ทีม ช นิด ข้อ มูล ทีส อดคล้อ งไป ่ ี ่ เท่า นัน ้  ยกเว้น กรณีท ี่argument นัน มีช นิด ข้อ มูล เป็น ้ String ซึง ในกรณีน จ ะสามารถส่ง ข้อ มูล ค่า คงทีไ ด้ ่ ี้ ่
  • 14. ตัว อย่า งโปรแกรมที่มีก ารส่ง argument ที่เ ป็น ออปเจ็ค import java.util.*; import java.util.*; public class StudentV1 { public class StudentV1 { String id; String id; String name; String name; Date dob; Date dob; public void setDOB(Date d) { public void setDOB(Date d) { dob = d; dob = d; }} public Date getDOB() { public Date getDOB() { return dob; return dob; }} }} ----------------------------------------------------------------- ----------------------------------------------------------------- ------------------------- ------------------------- public class TestStudentV1 { public class TestStudentV1 { public static void main(String args[]) { public static void main(String args[]) { StudentV1 s1 = new StudentV1(); StudentV1 s1 = new StudentV1(); Date d1 = new Date(16,12,1980); Date d1 = new Date(16,12,1980); s1.setDOB(d1); s1.setDOB(d1);
  • 15. ชนิด ข้อ มูล และจำา นวนของ argument  ชนิด ข้อ มูล ของ argument ที่จ ะส่ง ผ่า นไปยัง เมธอดไม่จ ำา เป็น ทีจ ะต้อ งเป็น ชนิด ข้อ มูล เดีย วกัน ่ แต่ต ้อ งเป็น ชนิด ข้อ มูล ทีส ามารถแปลงข้อ มูล ให้ ่ กว้า งขึ้น ได้โ ดยอัต โนมัต ิ  เมธอดใดๆอาจมี argument สำา หรับ รับ ค่า มากกว่า หนึง ตัว แต่ก ารเรีย กใช้เ มธอดเหล่า นีจ ะ ่ ้ ต้อ งส่ง argument ทีม ช นิด ข้อ มูล ทีส อดคล้อ งกัน ่ ี ่ และมีจ ำา นวนเท่า กัน
  • 16. ตัว อย่า งโปรแกรมที่มีก ารส่ง argument จำา นวนสองตัว public class NumericalSample { public class NumericalSample { public void calMax(int i, double d) { public void calMax(int i, double d) { if (i > d) { if (i > d) { System.out.println("Max = "+i); System.out.println("Max = "+i); } else { } else { System.out.println("Max = "+d); System.out.println("Max = "+d); }} }} public static void main(String args[]) { public static void main(String args[]) { NumericalSample obj = new NumericalSample(); NumericalSample obj = new NumericalSample(); obj.calMax(3,4.0); obj.calMax(3,4.0); }} }}
  • 17. การเปลีย นแปลงค่า ของ ่ argument  การส่ง argument ทีมีช นิด ข้อ มูล เป็น แบบพื้น ฐาน ่ หากมีก ารเปลี่ย นแปลงค่า ของ argument ภายในเมธอดทีถ ูก เรีย กใช้ง าน จะไม่ม ผ ลทำา ให้ ่ ี ค่า ของ argument ทีส ่ง ไปเปลี่ย นค่า ไปด้ว ย ่  การส่ง argument ทีม ช นิด ข้อ มูล เป็น แบบอ้า งอิง ่ ี จะเป็น การส่ง ตำา แหน่ง อ้า งอิง ของออปเจ็ค ไปให้ กับ เมธอดทีถ ูก เรีย กใช้ง าน ดัง นั้น การ ่ เปลี่ย นแปลงค่า ของคุณ ลัก ษณะขอ งออปเจ็ค จะมี ผลทำา ให้ค ่า ของคุณ ลัก ษณะของออปเจ็ค ทีส ่ง ไป ่ เปลี่ย นไปด้ว ย
  • 18. ตัว อย่า งโปรแกรมที่แ สดงการ เปลี่ย นแปลงค่า ของ argument import java.util.*; import java.util.*; public class ArgumentPassing { public class ArgumentPassing { public void method1(int x) { public void method1(int x) { x = 3; x = 3; }} public void method2(Date d) { public void method2(Date d) { d.setDate(1); d.setDate(1); d.setMonth(1); d.setMonth(1); d.setYear(2002); d.setYear(2002); }} public int method3() { public int method3() { return 0; return 0; }}
  • 19. ตัว อย่า งโปรแกรมที่แ สดงการ เปลี่ย นแปลงค่า ของ argument public static void main(String args[]) { public static void main(String args[]) { int x = 4; int x = 4; ArgumentPassing obj = new ArgumentPassing(); ArgumentPassing obj = new ArgumentPassing(); Date d1 = new Date(16,12,1980); Date d1 = new Date(16,12,1980); obj.method1(x); obj.method1(x); System.out.println("x = "+x); System.out.println("x = "+x); obj.method2(d1); obj.method2(d1); System.out.println("Date = "+d1); System.out.println("Date = "+d1); obj.method3(); obj.method3(); }} }}
  • 20. การรับ ค่า ที่ส ง กลับ มาจากเมธอด ่  เมธอดใดๆของคลาสสามารถทีจ ะมีค ่า ทีส ่ง กลับ มา ่ ่ ได้ ซึ่ง ชนิด ข้อ มูล ของค่า ทีจ ะส่ง กลับ อาจเป็น ชนิด ่ ข้อ มูล แบบพืน ฐาน หรือ เป็น ชนิด ข้อ มูล แบบอ้า งอิง ้  เมธอดทีม ค ่า ทีจ ะส่ง กลับ มาจะต้อ งมีค ำา สั่ง return ่ ี ่ ซึ่ง จะระบุค ่า ทีส ่ง กลับ โดยมีร ูป แบบดัง นี้ ่ return value;  คำา สั่ง ทีเ รีย กใช้เ มธอด ่ อาจจะรับ ค่า ทีส ่ง กลับ มา ่ เก็บ ไว้ใ นตัว แปรหรือ เป็น ตัว ถูก ดำา เนิน การใน นิพ จน์ ตัว อย่า งเช่น double d = getGPA(); System.out.println("GPA:"+getGPA());
  • 21. modifier ของเมธอด  modifier ของเมธอดประกอบด้ว ย • access modifier • static • abstract • synchronized • final  access modifier ใช้เ พื่อ ระบุร ะดับ การเข้า ถึง โดยมีค ีย เ วิร ์ด ต่า งๆดัง นี้ ์ • public • protected • private • default (ไม่ร ะบุุค ีย ์เ วิร ์ด ใดๆ)
  • 22. ตัว อย่า งโปรแกรมที่แ สดงการประกาศ เมธอดและคุณ ลัก ษณะ public class PrivateStudent { public class PrivateStudent { private String id; private String id; private String name; private String name; private double gpa; private double gpa; public void setDetails(String ID,String n,double GPA) { public void setDetails(String ID,String n,double GPA) { id = ID; id = ID; name = n; name = n; gpa = GPA; gpa = GPA; }} public void showDetails() { public void showDetails() { System.out.println("ID: "+id); System.out.println("ID: "+id); System.out.println("Name: "+name); System.out.println("Name: "+name); System.out.println("GPA: "+gpa); System.out.println("GPA: "+gpa); }} }}
  • 23. ตัว อย่า งโปรแกรมที่แ สดงการประกาศ เมธอดและคุณ ลัก ษณะ public class TestPrivateStudent { public class TestPrivateStudent { public static void main(String args[]) { public static void main(String args[]) { PrivateStudent ps = new PrivateStudent(); PrivateStudent ps = new PrivateStudent(); /* /* ps.id = "12345"; ps.id = "12345"; illegal illegal ps.name = "Thana"; ps.name = "Thana"; illegal illegal ps.gpa = 3.25; ps.gpa = 3.25; illegal */ illegal */ ps.setDetails("12345","Thana",3.25); ps.setDetails("12345","Thana",3.25); ps.showDetails(); ps.showDetails(); }} }}
  • 24. ตัว อย่า งโปรแกรม public class ThisStudent { public class ThisStudent { private String id; private String id; private String name; private String name; private double gpa; private double gpa; public void setDetails(String id,String name, double gpa) { public void setDetails(String id,String name, double gpa) { this.id = id; this.id = id; this.name = name; this.name = name; this.gpa = gpa; this.gpa = gpa; }} public void showDetails() { public void showDetails() { System.out.println("ID: "+this.id); System.out.println("ID: "+this.id); System.out.println("Name: "+name); System.out.println("Name: "+name); System.out.println("GPA: "+gpa); System.out.println("GPA: "+gpa); }} }}
  • 25. การเขีย นโปรแกรมโดยใช้ห ลัก การของการห่อ หุ้ม  คุณ ลัก ษณะเด่น ของโปรแกรมเชิง ออปเจ็ค มีอ ยู่ สามอย่า งคือ การห่อ หุม การสืบ ทอด และการมีไ ด้ ้ หลายรูป แบบ  ข้อ ดีข องการห่อ หุม ประการหนึง คือ การซ่อ นเร้น ้ ่ ข้อ มูล  หลัก การห่อ หุม ของออปเจ็ค ทำา ได้ด ัง นี้ ้ • กำา หนดคุณ ลัก ษณะของออปเจ็ค ให้มี modifier เป็น private เพื่อ ซ่อ นไม่ใ ห้อ อปเจ็ค อื่น ๆเรีย กใช้ไ ด้ • กำา หนดเมธอดของออปเจ็ค ที่ต ้อ งการให้อ อปเจ็ค อื่น ๆ เรีย กใช้ใ ห้มี modifier เป็น public
  • 26. ตัว อย่า งโปรแกรมที่ไ ม่ไ ด้ใ ช้ห ลัก การของการห่อ หุ้ม public class Student { public class Student { String ID; String ID; String name; String name; public double gpa; public double gpa; }} ----------------------------------------------------------------------- ----------------------------------------------------------------------- --------------------------- --------------------------- public class NoEncapDemo { public class NoEncapDemo { public static void main(String args[]) { public static void main(String args[]) { Student s1 = new Student(); Student s1 = new Student(); double temp = Double.parseDouble(args[0]); double temp = Double.parseDouble(args[0]); if ((temp<0) || (temp>4.00)) { if ((temp<0) || (temp>4.00)) { System.out.println("Incorrect Format!"); System.out.println("Incorrect Format!"); } else { } else { s1.gpa = temp; s1.gpa = temp; System.out.println("GPA: "+s1.gpa); System.out.println("GPA: "+s1.gpa); }} }} }}
  • 27. เมธอดแบบ setter  เมธอดแบบ accessor แบ่ง ออกได้เ ป็น สอง ประเภทคือ • เมธอดแบบ setter • เมธอดแบบ getter  เมธอดแบบ setter จะใช้ใ นการกำา หนดค่า ของ คุณ ลัก ษณะ  โดยทัว ไปชื่อ ของเมธอดแบบ ่ setter จะขึ้น ต้น ด้ว ยคำา ว่า set แล้ว ตามด้ว ยชื่อ ของคุณ ลัก ษณะ ซึ่ง มีร ูป แบบดัง นี้ public void setAttributeName(dataType arg) { attributeName = arg; }
  • 28. ตัว อย่า งโปรแกรมที่ใ ช้ห ลัก การ ของการห่อ หุ้ม public class Student {{ public class Student String ID; String ID; String name; String name; private double gpa; private double gpa; public void setGPA(double GPA) {{ public void setGPA(double GPA) if ((GPA<0) || (GPA>4.00)) {{ if ((GPA<0) || (GPA>4.00)) System.out.println("Incorrect Format!"); System.out.println("Incorrect Format!"); }} else {{ else gpa == GPA; gpa GPA; }} }} public double getGPA() {{ public double getGPA() return gpa; return gpa; }} }} ----------------------------------------------------------------------- ----------------------------------------------------------------------- --------------------------- --------------------------- public class EncapDemo { public class EncapDemo { public static void main(String args[]) {{ public static void main(String args[]) Student s1 == new Student(); Student s1 new Student(); double temp == Double.parseDouble(args[0]); double temp Double.parseDouble(args[0]); s1.setGPA(temp); s1.setGPA(temp); System.out.println("GPA: "+s1.getGPA()); System.out.println("GPA: "+s1.getGPA());
  • 29. เมธอดแบบ getter  เมธอดแบบ getter จะใช้ใ นการเรีย กค่า ของ คุณ ลัก ษณะ  โดยทัว ไปชื่อ ของเมธอดแบบ ่ getter จะขึน ต้น ้ ด้ว ยคำา ว่า get แล้ว ตามด้ว ยชื่อ ของคุณ ลัก ษณะ ซึ่ง มีร ูป แบบดัง นี้ public dataType getAttributeName() { return attributeName; }
  • 30. ตัว อย่า งโปรแกรมที่ใ ช้ห ลัก การ ของการห่อ หุ้ม public class EncapStudent { public class EncapStudent { private String id; private String id; private String name; private String name; private double gpa; private double gpa; public void setID(String ID) { public void setID(String ID) { id = ID; id = ID; }} public void setName(String n) { public void setName(String n) { name = n; name = n; }} public void setGPA(double GPA) { public void setGPA(double GPA) { if ((GPA<0) || (GPA>4.00)) { if ((GPA<0) || (GPA>4.00)) { System.out.println("Incorrect Format!"); System.out.println("Incorrect Format!"); } else { } else { gpa = GPA; gpa = GPA; }} }}
  • 31. ตัว อย่า งโปรแกรมที่ใ ช้ห ลัก การ ของการห่อ หุ้ม public String getID() { public String getID() { return id; return id; }} public String getName() { public String getName() { return name; return name; }} public double getGPA() { public double getGPA() { return gpa; return gpa; }} }}
  • 32. คีย ์เ วิร ์ด this  คีย เวิร ์ด this หมายถึง ออปเจ็ค ของตัว เอง  เราสามารถทีจ ะเรีย กใช้เ มธอดหรือ คุณ ลัก ษณะ ่ ภายในคลาสได้โ ดยใช้ คีย เ วิร ์ด ์ this ซึ่ง มีร ูป แบบดัง นี้ this.methodName(); this.attributeName  โดยทัว ไปเราจะไม่ใ ช้ค ีย เ วิร ์ด this ่ ์ ในคำา สั่ง ยกเว้น ในกรณีท จ ำา เป็น ี่
  • 33. การเขีย นโปรแกรมโดยใช้ห ลัก การของการสืบ ทอด  ข้อ ดีข องการสืบ ทอดคือ การนำา คลาสทีม อ ยูแ ล้ว มา ่ ี ่ ใช้ใ หม่โ ดยการเพิม เติม คุณ ลัก ษณะหรือ เมธอดใน ่ คลาสใหม่  การพัฒ นาคลาสขึ้น ใหม่ท ช ื่อ GradStudent ี่ สามารถทีจ ะเลือ กวิธ ีก ารได้ส องแบบคือ ่ • สร้า งคลาสขึ้น มาใหม่โ ดยไม่อ ้า งอิง กับ คลาสเดิม ที่ช ื่อ Student • สร้า งคลาสที่ส ืบ ทอดมาจากคลาสเดิม ที่ช ื่อ Student
  • 34. ตัว อย่า งโปรแกรม แสดงคลาสที่ช ื่อ Student public class Student { public class Student { private String id; private String id; private String name; private String name; private double gpa; private double gpa; public void setID(String ID) { public void setID(String ID) { id = ID; id = ID; }} public void setName(String n) { public void setName(String n) { name = n; name = n; }} public void setGPA(double GPA) { public void setGPA(double GPA) { gpa = GPA; gpa = GPA; }} public void showDetails() { public void showDetails() { System.out.println("ID: "+id); System.out.println("ID: "+id); System.out.println("Name: "+name); System.out.println("Name: "+name); System.out.println("GPA: "+gpa); System.out.println("GPA: "+gpa); }} }}
  • 35. ตัว อย่า งโปรแกรมที่ไ ม่ไ ด้ใ ช้ห ลัก การของการสืบ ทอด public class GradStudent { public class GradStudent { private String id; private String id; private String name; private String name; private double gpa; private double gpa; private String thesisTitle; private String thesisTitle; private String supervisor; private String supervisor; public void setID(String ID) { public void setID(String ID) { id = ID; id = ID; }} public void setName(String n) { public void setName(String n) { name = n; name = n; }} public void setGPA(double GPA) { public void setGPA(double GPA) { gpa = GPA; gpa = GPA; }}
  • 36. ตัว อย่า งโปรแกรมที่ไ ม่ไ ด้ใ ช้ห ลัก การของการสืบ ทอด public void setThesisTitle(String t) { public void setThesisTitle(String t) { thesisTitle = t; thesisTitle = t; }} public void setSupervisor(String s) { public void setSupervisor(String s) { supervisor = s; supervisor = s; }} public void showThesis() { public void showThesis() { System.out.println("ThesisTitle: "+thesisTitle); System.out.println("ThesisTitle: "+thesisTitle); System.out.println("Supervisor: "+supervisor); System.out.println("Supervisor: "+supervisor); }} }}
  • 37. ตัว อย่า งโปรแกรมที่ใ ช้ห ลัก การ ของการสืบ ทอด public class GradStudent extends Student { public class GradStudent extends Student { private String thesisTitle; private String thesisTitle; private String supervisor; private String supervisor; public void setThesisTitle(String t) { public void setThesisTitle(String t) { thesisTitle = t; thesisTitle = t; }} public void setSupervisor(String s) { public void setSupervisor(String s) { supervisor = s; supervisor = s; }} public void showThesis() { public void showThesis() { System.out.println("ThesisTitle: "+thesisTitle); System.out.println("ThesisTitle: "+thesisTitle); System.out.println("Supervisor: "+supervisor); System.out.println("Supervisor: "+supervisor); }} }}
  • 39. ตัว อย่า งการสืบ ทอดทีไ ม่ถ ูก ต้อ ง ่  เราสามารถทีต รวจสอบหลัก การของการสืบ ทอด ่ ด้ว ยคำา ว่า is a public class Shirt { char size; float price; } public class Skirt extends Shirt { boolean long; }
  • 40. ตัว อย่า งการสืบ ทอดที่ถ ูก ต้อ ง public class Clothing { char size; float price; } public class Shirt extends Clothing { } public class Skirt extends Clothing { boolean long; }
  • 41. คีย ์เ วิร ์ด protected  คุณ ลัก ษณะหรือ เมธอดของ superclass ทีม ี ่ modifier เป็น แบบ private จะทำา ให้ subclass ไม่ สามารถทีจ ะเรีย กใช้ไ ด้ ่  ภาษาจาวากำา หนดให้ม ี access modifier ทีช ื่อ ่ protected ซึง จะทำา ให้ subclass สามารถเรีย กใช้ ่ เมธอดหรือ คุณ ลัก ษณะของ superclass ได้  ตัว อย่า งเช่น protected String name;
  • 42. คีย ์เ วิร ์ด protected  ภาษาจาวากำา หนดให้ม ี access modifier ทีช ื่อ ่ protected ซึ่ง จะทำา ให้ค ลาสทีเ ป็น subclass ่ สามารถทีจ ะเรีย กใช้เ มธอดหรือ คุณ ลัก ษณะของ ่ superclass ได้ แม้ว ่า จะอยูต ่า งคลาสหรือ อยู่ต ่า ง ่ แพคเก็จ กัน ก็ต าม คลาสที่ อยู่ใ น คลาส คลาสที่ แพจ คลาส เดีย วกั เป็น su เก็จ ใดๆ น bclass เดีย วกั น public √ √ √ √
  • 43. ตัว อย่า งโปรแกรม public class Student { public class Student { protected String id; protected String id; protected String name; protected String name; protected double gpa; protected double gpa; public void setDetails(String ID,String n,double GPA) { public void setDetails(String ID,String n,double GPA) { id = ID; id = ID; name = n; name = n; gpa = GPA; gpa = GPA; }} public void showDetails() { public void showDetails() { System.out.println("ID: "+id); System.out.println("ID: "+id); System.out.println("Name: "+name); System.out.println("Name: "+name); System.out.println("GPA: "+gpa); System.out.println("GPA: "+gpa); }} }}
  • 44. คลาสที่ช ื่อ Object  ภาษาจาวาได้ก ำา หนดให้ค ลาสใดๆ สามารถจะ สืบ ทอดคลาสอื่น ได้เ พีย งคลาสเดีย วเท่า นัน ้  คลาสทุก คลาสในภาษาจาวาถ้า ไม่ไ ด้ส ืบ ทอดจาก คลาสใดเลยจะถือ ว่า คลาสนั้น สืบ ทอดจากคลาสที่ ชื่อ Object  ตัว อย่า งเช่น public class Student extends Object { ... }  คลาสทีช ื่อ Object ่ จะมีเ มธอดทีส ำา คัญ คือ ่ public String toString() และ public boolean equals(Object o)
  • 45. คีย ์เ วิร ์ด super  super เป็น คีย เ วิร ์ด ทีใ ช้ใ นการอ้า งอิง ถึง ์ ่ superclass เพือ ทีจ ะเรีย กใช้ เมธอดหรือ ่ ่ constructor ของ superclass โดยมีร ูป แบบคำา สั่ง ดัง นี้ super.methodName([arguments])  ตัว อย่า งเช่น คลาส GradStudent อาจมีเ มธอดทีช อ่ ื่ showDetails() โดยมีค ำา สัง ทีเ รีย กใช้เ มธอด ่ ่ showDetails() ของคลาส Student และ showThesis() ของคลาส GradStudent public void showDetails() { super.showDetails(); showThesis(); }
  • 46. การเขีย นโปรแกรมโดยใช้ห ลัก การของ การมีไ ด้ห ลายรูป แบบ  การมีไ ด้ห ลายรูป แบบหมายถึง คุณ สมบัต ิข องออป เจ็ค ของคลาสทีต ่า งกัน สามารถตอบสนองต่อ ่ เมธอดเดีย วกัน ในวิธ ีก ารที่ต า งกัน ได้ ซึ่ง หมายถึง ่ การเขีย นเมธอดแบบ overridden และการใช้ Dynamic Binding  การเขีย นเมธอดแบบ overridden มีข ้อ กำา หนด ดัง นี้ • จำา นวนและชนิด ข้อ มูล ของ argument จะต้อ งเหมือ น เดิม • ชนิด ข้อ มูล ของค่า ที่ส ่ง กลับ จะต้อ งเหมือ นเดิม • access modifier จะต้อ งไม่ม ีร ะดับ ตำ่า กว่า เดิม อาทิเ ช่น ถ้า เมธอดเดิม เป็น public จะไม่ส ามารถเปลี่ย นเป็น private ได้
  • 47. Dynamic Binding  ข้อ ดีข องการมีไ ด้ห ลายรูป แบบอีก ประการหนึง ่ คือ การทีส ามารถกำา หนดออปเจ็ค ได้ห ลายรูป แบบ ่ ตัว อย่า งเช่น จากรูป แบบ SuperClass obj;  เราสามารถทีจ ะสร้า งออปเจ็ค ทีช อ obj ่ ่ ื่ ซึ่ง อาจเป็น ออปเจ็ค ของคลาสทีเ ป็น superclass หรือ ทีเ ป็น ่ ่ subclass ได้ โดยใช้ค ำา สัง ทีม ร ูป แบบดัง นี้ ่ ่ ี obj = new SuperClass(); หรือ obj = new SubClass();  ตัว อย่า งเช่น Student s; s = new GradStudent();
  • 48. รูป แสดงการมีไ ด้ห ลายรูป แบบ  ตัว อย่า ง Student s1 = new Student(); Student s1 = new GradStudent(); Student s1 = new PhDStudent();
  • 49. เมธอดแบบ Overloaded  ภาษาจาวาอนุญ าตให้ค ลาสใดๆมีเ มธอดทีม ช อ ่ ี ื่ เดีย วกัน มากกว่า หนึง เมธอดได้ แต่เ มธอดเหล่า นั้น ่ จะต้อ งมีจ ำา นวนหรือ ชนิด ข้อ มูล ของ argument ที่ ต่า งกัน  ตัว อย่า งต่อ ไปนี้เ ป็น การเขีย นเมธอดแบบ overloaded ทีถ ูก ต้อ ง ่ public void setDetails(String ID, String n) { } public void setDetails(String ID, double GPA) { } public double setDetails(double GPA, String n) { }  ตัว อย่า งต่อ ไปนี้เ ป็น การเขีย นเมธอดแบบ overloaded ทีไ ม่ถ ก ต้อ ง ่ ู
  • 50. ตัว อย่า งโปรแกรม public class StudentV2 { public class StudentV2 { private String id; private String id; private String name; private String name; private double gpa; private double gpa; public void setDetails(String ID,String n) { public void setDetails(String ID,String n) { id = ID; id = ID; name = n; name = n; }} public void setDetails(String ID,String n,double GPA) { public void setDetails(String ID,String n,double GPA) { id = ID; id = ID; name = n; name = n; gpa = GPA; gpa = GPA; }} }}
  • 51. เมธอดแบบ overridden  การกำา หนดเมธอดแบบ overridden เป็น หลัก การ ทีส บ เนือ งมาจากหลัก การของการสืบ ทอด โดย ่ ื ่ คลาสทีเ ป็น subclass สามารถทีจ ะเขีย นเมธอด ่ ่ ของ superclass ขึ้น ใหม่ไ ด้ วิธ ีก ารนีเ รีย กว่า ้ เมธอดใน subclass เป็น เมธอดแบบ overridden  การเขีย นเมธอดแบบ overridden มีข ้อ กำา หนด ดัง นี้ • จำา นวนและชนิด ข้อ มูล ของ argument จะต้อ งเหมือ น เดิม • ชนิด ข้อ มูล ของค่า ที่ส ่ง กลับ จะต้อ งเหมือ นเดิม • access modifier จะต้อ งไม่ม ีร ะดับ ตำ่า กว่า เดิม อาทิเ ช่น ถ้า เมธอดเดิม เป็น public จะไม่ส ามารถเปลี่ย นเป็น private ได้
  • 52. ตัว อย่า ง โปรแกรมแสดงเมธอดแบบ overridden class Student { class Student { :: public void showDetails() { public void showDetails() { System.out.println("ID: "+id); System.out.println("ID: "+id); System.out.println("Name: "+name); System.out.println("Name: "+name); System.out.println("GPA: "+gpa); System.out.println("GPA: "+gpa); }} }} public class GradStudentV2 extends Student { public class GradStudentV2 extends Student { :: public void showDetails() { public void showDetails() { super.showDetails(); super.showDetails(); System.out.println("ThesisTitle: "+thesisTitle); System.out.println("ThesisTitle: "+thesisTitle); System.out.println("Supervisor: "+supervisor); System.out.println("Supervisor: "+supervisor); }} }}
  • 53. Virtual Method Invocation  โปรแกรมภาษาจาวาพิจ ารณาเรีย กใช้เ มธอดจาก ชนิด ของออปเจ็ค ทีส ร้า งขึ้น ตัว อย่า งเช่น คำา สัง ่ ่ Student s1 = new GradStudent(); s1.showDetails(); เป็น คำา สั่ง ทีเ รีย กใช้เ มธอด showDetails() ของคลาส ่ GradStudent ไม่ใ ช่เ มธอดของคลาส Student  แต่ค อมไพเลอร์ข องภาษาจาวาจะไม่อ นุญ าตให้ เรีย กใช้เ มธอดใดๆก็ต ามทีไ ม่ม ก ารประกาศอยูใ น ่ ี ่ เมธอดของ superclass ทีก ำา หนดไว้ ตัว อย่า งเช่น ่ Student s1 = new GradStudent(); s1.getSupervisor(); // Fail to compile
  • 54. การส่ง ผ่า น argument ได้ห ลาย รูป แบบ  ในกรณีท เ มธอดมี ี่ argument เป็น ข้อ มูล ชนิด คลาส เราสามารถทีจ ะส่ง ออปเจ็ค ของคลาสที่ ่ เป็น subclass ของคลาสนัน แทนได้ ตัว อย่า งเช่น ้ คำา สั่ง Student s1 = new Student(); PhDStudent s2 = new GradStudent();  เราสามารถทีจ ะเรีย กใช้เ มธอด printInfo(Student ่ s)ได้ห ลายรูป แบบดัง นี้ printInfo(s1) printInfo(s2)  หรือ printInfo(new FullTimeStudent())
  • 55. ตัว ดำา เนิน การ instanceof  คีย เ วิร ์ด instanceof ์ เป็น ตัว ดำา เนิน การทีใ ช้ก ับ ออป ่ เจ็ค และคลาส เพือ ตรวจสอบว่า เป็น ออปเจ็ค ของ ่ คลาสนัน หรือ ไม่ โดย จะให้ผ ลลัพ ธ์เ ป็น ข้อ มูล ชนิด ้ boolean  ถ้า ได้ผลลัพ ธ์เ ป็น true แสดงว่า ออปเจ็ค เป็น ของ คลาสนัน หรือ เป็น ของคลาสที่ค ลาสนัน สืบ ทอดมา ้ ้  ตัว อย่า งเช่น GradStudent s1 = new GradStudent(); (s1 instanceof GradStudent) → true (s1 instanceof Student) → true (s1 instanceof Object) → true (s1 instanceof String) → false
  • 56. ตัว อย่า ง เมธอดที่แ สดงการใช้ instanceof public void printInfo(Student s) { public void printInfo(Student s) { if (s instanceof PhDStudent) { if (s instanceof PhDStudent) { System.out.println("PhD Student"); System.out.println("PhD Student"); } else if (s instanceof GradStudent) { } else if (s instanceof GradStudent) { System.out.println("Graduate Student"); System.out.println("Graduate Student"); } else if (s instanceof FullTimeStudent) { } else if (s instanceof FullTimeStudent) { System.out.println("Full-Time Student"); System.out.println("Full-Time Student"); } else if (s instanceof PartTimeStudent) { } else if (s instanceof PartTimeStudent) { System.out.println("Part-Time Student"); System.out.println("Part-Time Student"); } else if (s instanceof Student) { } else if (s instanceof Student) { System.out.println("Student"); System.out.println("Student"); }}
  • 57. การ Casting ออปเจ็ค  การ Casting ออปเจ็ค จะทำา ให้ต ัว แปร ของ subclass สามารถทีจ ะอ้า งอิง ถึง ออปเจ็ค ของ ่ subclass ผ่า นตัว แปรของ superclass ได้ อาทิ เช่น Student s1 = new GradStudent(); GradStudent s2 = (GradStudent)s1;  ภาษาจาวาจะตรวจสอบชนิด ของออปเจ็ค ในช่ว ง ของ การรัน โปรแกรม ดัง นัน ตัว แปร ของ subclass ้ จะไม่ส ามารถอ้า งอิง ถึง ออปเจ็ค ของ superclass ได้ แม้ว ่า จะทำา การ casting ออปเจ็ค แล้ว ก็ต าม อาทิเ ช่น Student s1 = new Student(); GradStudent s2 = (GradStudent)s1;
  • 58. การเรีย กใช้เ มธอดของออปเจ็ค  การเรีย กใช้เ มธอดของออปเจ็ค ทีท ำา การ ่ casting ภาษาจาวาจะตรวจสอบชนิด ของออปเจ็ค ในช่ว ง ของการรัน โปรแกรม ว่า ออปเจ็ค ดัง กล่า วเป็น ออปเจ็ค ของคลาสใด และคลาสนั้น มีเ มธอดทีเ รีย ก่ ใช้ห รือ ไม่ หากไม่พ บก็จ ะเกิด ข้อ ผิด พลาดในช่ว ง ของการรัน โปรแกรม ตัว อย่า งเช่น Student s1 = new Student(); ((GradStudent) s1).getSupervisor(); // คอมไพล์ผ ่า นได้ แต่จ ะเกิด ข้อ ผิด พลาดในช่ว งของ การรัน โปรแกรม
  • 59. ตัว อย่า งโปรแกรมแสดงการเรีย ก ใช้เ มธอด public class Student { public class Student { private String id; private String id; private String name; private String name; private double gpa; private double gpa; public String getID() { public String getID() { return id; return id; }} public String getName() { public String getName() { return name; return name; }} public double getGPA() { public double getGPA() { return gpa; return gpa; }} }}
  • 60. ตัว อย่า งโปรแกรมแสดงการเรีย ก ใช้เ มธอด public class GradStudent extends Student { public class GradStudent extends Student { private String thesisTitle; private String thesisTitle; private String supervisor; private String supervisor; public String getThesisTitle() { public String getThesisTitle() { return thesisTitle; return thesisTitle; }} public String getSupervisor() { public String getSupervisor() { return supervisor; return supervisor; }} }}
  • 61. ตัว อย่า งโปรแกรมแสดงการเรีย ก ใช้เ มธอด public class TestCallingMethods { public class TestCallingMethods { public static void main(String args[]) { public static void main(String args[]) { Student s1 = new GradStudent(); Student s1 = new GradStudent(); s1.getSupervisor(); s1.getSupervisor(); // compile error // compile error Student s2 = new Student(); Student s2 = new Student(); ((GradStudent) s2).getSupervisor(); ((GradStudent) s2).getSupervisor(); // runtime error // runtime error }} }}
  • 62. Constructor  constructor เป็น เมธอดทีม ช ื่อ เดีย วกับ ชื่อ คลาส ่ ี ซึ่ง มีร ูป แบบดัง นี้ [modifier] ClassName([arguments]) { [statements] }  constructor เป็น เมธอดทีไ ม่ม ค ่า ทีจ ะส่ง กลับ แต่ ่ ี ่ ไม่ต ้อ งระบุค ีย เ วิร ์ด void ์  โดยทัว ไปคลาสทุก คลาสจะมี ่ default constructor ซึ่ง มีร ูป แบบดัง นี้ public ClassName() { }  เราสาม ารถทีจ ะเขีย น ่ constructor ในคลาสใดๆ
  • 63. ตัว อย่า งโปรแกรมแสดงการเรีย ก ใช้ constructor public class Student { public class Student { private String id; private String id; private String name; private String name; public Student(String ID,String n) { public Student(String ID,String n) { id = ID; id = ID; name = n; name = n; }} }} public class TestConstructor { public class TestConstructor { public static void main(String args[]) { public static void main(String args[]) { Student s = new Student(); Student s = new Student(); //illegal //illegal Student s = new Student("1122","Somchai"); Student s = new Student("1122","Somchai"); }} }}
  • 64. คำา สั่ง new()  มีข ั้น ตอนการทำา งานดัง นี้ • กำา หนดเนื้อ ที่ใ นหน่ว ยความจำา ให้ก ับ ออปเจ็ค • กำา หนดค่า เริ่ม ต้น ให้ก ับ คุณ ลัก ษณะของออปเจ็ค • กำา หนดค่า ของคุณ ลัก ษณะของออปเจ็ค ตามคำา สั่ง กำา หนดค่า ที่ป ระกาศไว้ • เรีย กใช้ constructor  ตัว อย่า งเช่น public class MyDate { private int day = 1; private int month = 1; private int year = 2000; public MyDate(int d,int m,int y) { day = d; month = m; year = y; } }
  • 65. รูป แสดงตัว อย่า งการทำา งานขั้น ตอนคำา สั่ง new  คำา สั่ง MyDate d1 = new MyDate(16,8,1972); ขั้น ตอนที่ห นึ่ง ขั้น ตอนที่ส อง ขั้น ตอนที่ส าม ขั้น ตอนที่ส ี่
  • 66. Constructor แบบ Overloaded  เราสามารถทีจ ะสร้า ง ่ constructor แบบ overloaded ของคลาสได้  constructor แบบ overloaded จะมีจ ำา นวนหรือ ชนิด ข้อ มูล ทีแ ตกต่า งกัน ของ arguments ่  คำา สั่ง new ทีใ ช้ใ นการสร้า งออปเจ็ค จะเรีย กใช้ ่ constructor ทีม ี argument ทีส ง ผ่า นมาสอด ่ ่ ่ คล้อ งกับ constructor ทีก ำา หนด ่  ข้อ ดีข องการสร้า ง constructor แบบ overloaded คือ ทำา ให้เ ราสามารถทีส ร้า งออปเจ็ค ่ เริ่ม ต้น ได้ห ลายรูป แบบ  เมธอดทีช ื่อ this() ่ เป็น การเรีย กใช้ constructor
  • 67. ตัว อย่า งโปรแกรมแสดง constructor แบบ overloaded public class Student { public class Student { private String id; private String id; private String name; private String name; private double gpa; private double gpa; public Student(String ID,String n) { public Student(String ID,String n) { id = ID; id = ID; name = n; name = n; }} public Student(String ID,String n,double GPA) { public Student(String ID,String n,double GPA) { id = ID; id = ID; name = n; name = n; gpa = GPA; gpa = GPA; }} }}
  • 68. เมธอด this()  เมธอดทีช ื่อ this() ่ เป็น การเรีย กใช้ constructor ของคลาสตัว เอง โดยจะต้อ งเป็น คำา สัง แรกสุด ทีอ ยู่ ่ ่ ใน constructor แบบ overloaded
  • 69. ตัว อย่า งโปรแกรมแสดงการใช้ เมธอด this() public class Student { public class Student { private String id; private String id; private String name; private String name; private double gpa; private double gpa; public Student(String ID,String n) { public Student(String ID,String n) { id = ID; id = ID; name = n; name = n; }} public Student(String ID,String n,double GPA) { public Student(String ID,String n,double GPA) { this(ID,n); this(ID,n); gpa = GPA; gpa = GPA; }} }}
  • 70. เมธอด super()  constructorของ superclass จะไม่ส บ ทอด ื มายัง subclass  เราสามารถทีจ ะเรีย กใช้ ่ constructor ของ superclass ได้โ ดยใช้เ มธอดทีช ื่อ ่ super() โดยส่ง ผ่า น argument ทีส อดคล้อ งกัน ่  เมธอด super() จะต้อ งเป็น คำา สัง แรกของ ่ constructor เช่น เดีย วกับ เมธอดทีช อ่ ื่ this()  โดยทัว ไปการทำา งานของ ่ constructor ของ คลาสใดๆจะมีผ ลทำา ให้ม ก ารเรีย กใช้ ี constructor ของ superclass นัน ซึ่ง ถ้า ไม่ม ค ำา ้ ี สั่ง super() อยูใ นคำา สั่ง แรกของ constructor ของ ่
  • 71. ตัว อย่า ง คลาสทีม ี constructor ใน ่ รูป แบบต่า งๆ public class Student { public class Student { protected String name; protected String name; public Student(String n) { public Student(String n) { name = n; name = n; }} }} public class GradStudent extends Student { public class GradStudent extends Student { public GradStudent(String n) { public GradStudent(String n) { super(n); super(n); }} public GradStudent() { public GradStudent() { this(" "); this(" "); }} }}
  • 72. ขั้น ตอนการทำา งานของ constructor 1. ถ้า มีค ำา สั่ง this() ใน constructor ก็จ ะเรีย กใช้ constructor แบบ overloaded ทีส อดคล้อ งกับ ่ คำา สัง this() แล้ว ข้า มไปขั้น ตอนที่ 4 ่ 2. เรีย กใช้ constructor ของ superclass ถ้า ไม่ม ี คำา สัง super() จะเรีย กใช้ constructor แบบ ่ default ยกเว้น คลาสทีช อ Object จะไม่ม ก าร ่ ื่ ี เรีย กใช้ constructor ของ superclass เนื่อ งจากคลาสทีช ื่อ Object จะไม่ม ี superclass ่ 3. เรีย กใช้ค ำา สั่ง กำา หนดค่า เริ่ม ต้น ของคุณ ลัก ษณะ ของออปเจ็ค 4. เรีย กใช้ค ำา สั่ง ภายใน constructor ของคลาสที่
  • 73. เมธอดของคลาสที่ช ื่อ Object  คลาสทีช ื่อ Object ่ จะเป็น คลาสทีท ก ๆคลาสจะ ่ ุ สืบ ทอดมา  เมธอดของคลาส Object ทีส ำา คัญ คือ ่ • toString() เป็น เมธอดที่ใ ช้ใ นการแปลงค่า ของออปเจ็ค ให้เ ป็น String ซึ่ง จะส่ง ค่า กลับ เป็น ข้อ มูล ชนิด คลาส String • equals(Object o) เป็น เมธอดที่ใ ช้ใ นการเปรีย บเทีย บค่า ของออปเจ็ค ซึ่ง จะส่ง ค่า กลับ เป็น ข้อ มูล ชนิด boolean
  • 74. เมธอด toString()  คลาสทีต ้อ งการจะแปลงข้อ มูล เป็น String ่ จะต้อ งมี เมธอดแบบ overriden ทีช อ toString() ่ ื่  เช่น คลาส Date ได้ก ำา หนดคำา สั่ง สำา หรับ เมธอด toString() ไว้แ ล้ว ดัง นัน การเรีย กใช้เ มธอด ้ System.out.println()โดยที่ argument เป็น ออป เจ็ค ของคลาส Date จะทำา ให้ม ก ารเรีย กใช้เ มธอด ี toString() ของคลาส Date โดยอัต โนมัต ิ ตัว อย่า ง เช่น คำา สัง ่ Date d = new Date(); System.out.println(d);
  • 75. ตัว อย่า ง โปรแกรมแสดงเมธอด toString() public class Student { public class Student { private String name; private String name; public Student(String n) { public Student(String n) { name = n; name = n; }} public String toString() { public String toString() { return name; return name; }} }} public class TestToString { public class TestToString { public static void main(String args[]) { public static void main(String args[]) { Student s1 = new Student("Thana"); Student s1 = new Student("Thana"); System.out.println(s1); System.out.println(s1); }} }} ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม ี่ Thana
  • 76. เมธอด equals()  คลาสทีต ้อ งการสร้า งเมธอดเพือ เปรีย บเทีย บ ค่า ขอ ่ ่ ง ออปเจ็ค ว่า เท่า กัน หรือ ไม่ จะต้อ ง มีเ มธอดแบบ overriden ทีช อ equals() ่ ื่
  • 77. ตัว อย่า ง โปรแกรมแสดงเมธอด equals() class Student { class Student { private String name; private String name; public Student(String n) { public Student(String n) { name = n; name = n; }} public boolean equals(Object obj) { public boolean equals(Object obj) { if (obj.equals(name)) { return true;} if (obj.equals(name)) { return true;} else return false; else return false; }} }} public class TestEquals { public class TestEquals { public static void main(String args[]) { public static void main(String args[]) { Student s1 = new Student("Thana"); Student s1 = new Student("Thana"); Student s2 = new Student("Thana"); Student s2 = new Student("Thana"); System.out.println(s1.equals(s2)); System.out.println(s1.equals(s2)); }} }} ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม ี่ true
  • 78. คลาสประเภท Wrapper  คลาสประเภท Wrapper จะช่ว ยในการสร้า งออป เจ็ค ทีเ ก็บ ชนิด ข้อ มูล แบบพืน ฐานไว้ใ นคอลเล็ก ชั่น ่ ้ แบบ Heterogeneous  คลาสประเภท ชนิด ข้อ มูล มีด ัง นี้ Wrapper คลาส boolean Boolean byte Byte short Short int Integer long Long float Float double Double char Character
  • 79. ตัว อย่า งโปรแกรมแสดงการใช้ คลาสประเภท Wrapper public class ShowWrapper { public class ShowWrapper { public static void main(String args[]) { public static void main(String args[]) { Boolean b = new Boolean(true); Boolean b = new Boolean(true); Character c = new Character('A'); Character c = new Character('A'); Double d = new Double(1.234); Double d = new Double(1.234); System.out.println(b); System.out.println(b); System.out.println(c); System.out.println(c); System.out.println(d); System.out.println(d); }} }} true ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม ี่ A 1.234
  • 80. คุณ ลัก ษณะแบบ static  คีย เ วิร ์ด static ์ สามารถทีจ ะใช้ก ับ คุณ ลัก ษณะและ ่ เมธอดของคลาสได้  คุณ ลัก ษณะของคลาสทีเ ป็น แบบ static ่ จะเป็น คุณ ลัก ษณะทีท ก ออปเจ็ค ของคลาสนี้ใ ช้ร ่ว มกัน ่ ุ  คุณ ลัก ษณะของคลาสทีเ ป็น แบบ static ่ สามารถ ถูก เรีย กใช้จ ากชื่อ ของคลาสได้โ ดยไม่จ ำา เป็น ต้อ ง สร้า งออปเจ็ค อาทิเ ช่น • Student.counter
  • 81. ตัว อย่า งโปรแกรมแสดงการใช้ คีย ์เ วิร ์ด static public class Student { public class Student { static int counter; static int counter; public Student() { public Student() { counter++; counter++; }} }} public class TestStatic { public class TestStatic { public static void main(String args[]) { public static void main(String args[]) { Student s1 = new Student(); Student s1 = new Student(); Student s2 = new Student(); Student s2 = new Student(); System.out.println(Student.counter); System.out.println(Student.counter); System.out.println(s1.counter); System.out.println(s1.counter); System.out.println(s2.counter); System.out.println(s2.counter); }} }} 2 ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม ี่ 2 2
  • 82. เมธอดแบบ static  เมธอดโดยทัว ไปจะ มี ่ modifier เป็น แบบ non- static  เมธอดทีม ี ่ modifier เป็น แบบ static จะสามารถ ถูก เรีย กใช้ง าน โดยใช้ช อ คลาสได้เ ลยไม่จ ำา เป็น ื่ ต้อ งสร้า งออปเจ็ก ของคลาสนั้น ขึ้น มาก่อ น ซึ่ง มีร ูป แบบดัง นี้ className.methodName();  ตัว อย่า งเช่น เมธอดทุก เมธอดในคลาส Math เป็น แบบ static ดัง นัน การเรีย กใช้ง านทุก เมธอดใน ้ คลาสสามารถทำา ได้ เช่น Math.sqrt(4);  เมธอดแบบ static จะไม่ส ามารถเรีย กใช้เ มธอด
  • 83. Static Initializer  StaticInitializer คือ บล็อ กในคลาสใดๆทีอ ยู่ ่ นอกเมธอด และมีค ีย เ วิร ์ด static เพือ นิย ามให้เ ป็น ์ ่ บล็อ กแบบ static  รูป แบบของ Static Initializer static { ... }  คำา สั่ง ในบล็อ กแบบ static จะถูก เรีย กใช้เ พีย งครั้ง เดีย วเมือ JVM โหลดคลาสดัง กล่า วขึ้น มา ่ Initializer ใช้ใ นการ ดีบ ัก (debug)  Static โปรแกรม หรือ ใช้ใ นกรณีท ต ้อ งการสร้า งออปเจ็ค ี่ ของคลาสขึ้น โดยอัต โนมัต ิ
  • 84. ตัว อย่า งโปรแกรมแสดง Static Initializer public class TestStaticBlock { public class TestStaticBlock { static int x=5; static int x=5; static { static { x += 1; x += 1; }} public static void main(String args[]) { public static void main(String args[]) { System.out.println("x = "+x); System.out.println("x = "+x); }} static { static { x /= 2; x /= 2; }} }} ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม 3 ี่ x =
  • 85. คีย ์เ วิร ์ด final  คีย เ วิร ์ด final ์ สามารถจะใช้ไ ด้ก บ คลาส ตัว แปร ั และเมธอด  คลาสทีม ี ่ modifier เป็น final จะทำา ให้ค ลาสอื่น ไม่ส ามารถสืบ ทอดคลาสนี้ไ ด้  เมธอดทีม ี ่ modifier เป็น final คือ เมธอดที่จ ะไม่ สามารถมีเ มธอดแบบ overriden ได้  ตัว แปรทีม ี ่ modifier เป็น final คือ ค่า คงที่ ซึ่ง จะ ทำา ให้ส ามารถกำา หนดค่า ได้เ พีย งครั้ง เดีย วเท่า นัน ้
  • 86. คลาสแบบ abstract  คลาสทีม ี ่ modifier เป็น abstract หมายความว่า คลาสนัน ยัง เป็น คลาสทีไ ม่ส มบูร ณ์ โดยมีเ มธอด ้ ่ แบบ abstract ซึ่ง เป็น เมธอดทีย ัง ไม่ส มบูร ณ์อ ย่า ง ่ น้อ ยหนึง เมธอดอยู่ใ นคลาส ่  รูป แบบของเมธอดแบบ abstract [modifier] abstract return_type methodName([arguments]);  คลาสแบบ abstract กำา หนดขึ้น มาเพือ ให้ค ลาสอื่น ่ สืบ ทอด โดยคลาสทีม าสืบ ทอดจะต้อ งกำา หนด ่ บล็อ กคำา สั่ง ในเมธอดทีย ง ไม่ส มบูร ณ์ ่ ั  เราไม่ส ามารถสร้า งออปเจ็ค ของคลาสแบบ abstract ได้
  • 87. ตัว อย่า งโปรแกรมแสดงคลาสแบบ abstract public abstract class Student { public abstract class Student { protected String id; protected String id; protected String name; protected String name; protected double gpa; protected double gpa; public void setID(String ID) { public void setID(String ID) { id = ID; id = ID; }} public void setName(String n) { public void setName(String n) { name = n; name = n; }} public void setGPA(double GPA) { public void setGPA(double GPA) { gpa = GPA; gpa = GPA; }} public abstract void showDetails(); public abstract void showDetails(); }}
  • 88. ตัว อย่า งคลาสที่ส บ ทอดมาจาก ื คลาสแบบ abstract public class FullTimeStudent extends Student { public class FullTimeStudent extends Student { private int credit; private int credit; private final int MAX_YEAR = 4; private final int MAX_YEAR = 4; public FullTimeStudent(int c) { public FullTimeStudent(int c) { credit = c; credit = c; }} public void showDetails() { public void showDetails() { System.out.println("ID: "+id); System.out.println("ID: "+id); System.out.println("Name: "+name); System.out.println("Name: "+name); System.out.println("GPA: "+gpa); System.out.println("GPA: "+gpa); System.out.println("Credit: "+credit); System.out.println("Credit: "+credit); }} }}
  • 89. อิน เตอร์เ ฟส  อิน เตอร์เ ฟส (interface) มีล ัก ษณะคล้า ยกับ คลาส แบบ abstract แต่จ ะประกอบด้ว ยเมธอดทีย ง ไม่ ่ ั สมบูร ณ์เ ท่า นัน ้  รูป แบบของอิน เตอร์เ ฟส [modifier] interface InterfaceName { [methods();] }  อิน เตอร์เ ฟสกำา หนดขึ้น มาเพือ ให้ค ลาสอื่น นำา ไป ่ ใช้ง านโดยใช้ค ีย เ วิร ์ด ์ implements โดยมีร ูป แบบ ดัง นี้ [modifier] class ClassName implements InterfaceName { [methods();]
  • 90. อิน เตอร์เ ฟส  อิน เตอร์เ ฟสจะเหมือ นกับ คลาสแบบ abstractตรงที่ เราจะไม่ส ามารถสร้า งออปเจ็ค ของอิน เตอร์เ ฟสได้  ประโยชน์ข องอิน เตอร์เ ฟสคือ • การกำา หนดรูป แบบของเมธอดต่า งๆที่ค ลาสอื่น ๆจะต้อ ง implements ไว้ล ่ว งหน้า ซึ่ง สามารถอาศัย หลัก การของ การมีไ ด้ห ลายรูป แบบมาเรีย กใช้เ มธอดเหล่า นั้น ได้จ าก คลาสที่ implements อิน เตอร์เ ฟส • ภาษาจาวากำา หนดให้ค ลาสใดๆสามารถสืบ ทอดคลาส อื่น ได้เ พีย งคลาสเดีย วเท่า นั้น แต่จ ะสามารถ implements อิน เตอร์เ ฟสได้ห ลายอิน เตอร์เ ฟส
  • 91. ตัว อย่า งอิน เตอร์เ ฟส public interface Student { public interface Student { public void setID(String ID); public void setID(String ID); public void setName(String n); public void setName(String n); public void setGPA(double GPA); public void setGPA(double GPA); public void showDetails(); public void showDetails(); }}
  • 92. ตัว อย่า ง คลาสที่ implements อิน เตอร์เ ฟส public class PartTimeStudent implements Student { public class PartTimeStudent implements Student { private String id; private String id; private String name; private String name; private double gpa; private double gpa; private int credit; private int credit; private final int MAX_YEAR = 8; private final int MAX_YEAR = 8; public PartTimeStudent(int c) { public PartTimeStudent(int c) { credit = c; credit = c; }} public void setID(String ID) { public void setID(String ID) { id = ID; id = ID; }} public void setName(String n) { public void setName(String n) { name = n; name = n; }}
  • 93. ตัว อย่า ง คลาสที่ implements อิน เตอร์เ ฟส public void setGPA(double GPA) { public void setGPA(double GPA) { gpa = GPA; gpa = GPA; }} public void showDetails() { public void showDetails() { System.out.println("ID: "+id); System.out.println("ID: "+id); System.out.println("Name: "+name); System.out.println("Name: "+name); System.out.println("GPA: "+gpa); System.out.println("GPA: "+gpa); System.out.println("Credit: "+credit); System.out.println("Credit: "+credit); }} }}
  • 94. คลาสภายใน  คลาสภายใน (Inner class) คือ คลาสทีป ระกาศ ่ อยูภ ายในคลาสอื่น ๆ ซึง บางครั้ง เรีย กว่า คลาส ่ ่ แบบซ้อ น (nested class)  คลาสภายในอนุญ าตให้ป ระกาศคุณ ลัก ษณะหรือ เมธอดภายในคลาสอื่น ได้  คลาสภายในมีป ระโยชน์ใ นกรณีท ต ้อ งการจะจัด ี่ กลุ่ม ของคลาสทีต ้อ งทำา งานร่ว มกัน โดยต้อ งการ ่ ควบคุม ไม่ใ ห้ม ก ารเข้า ถึง โดยตรงจากคลาสอื่น ๆ ี และต้อ งการเรีย กใช้ค ุณ ลัก ษณะหรือ เมธอดของ คลาสทีอ ยู่ภ ายนอกได้โ ดยตรง ่  คลาสภายในทีใ ช้ใ นภาษาจาวาแบ่ง ออกเป็น ่ • คลาสภายในที่อ ยู่ภ ายในคลาสโดยตรง
  • 95. คลาสภายในทีอ ยู่ภ ายในคลาส ่  กรณีน เ ป็น การประกาศคลาสภายในคลาสอื่น ที่ ี้ เรีย กว่า คลาสภายนอก (Outer class)  คลาสภายในสามารถมี access modifier เป็น public, private, default หรือ protected ได้  การสร้า งออปเจ็ค ของคลาสภายในมีข อ แตกต่า ง ้ ดัง นี้ • การสร้า งออปเจ็ค ของคลาสภายในนอกคลาสภายนอก จะต้อ งทำา การสร้า งออปเจ็ค ของคลาสภายนอกก่อ น แล้ว จึง สร้า งออปเจ็ค ของคลาสภายในได้ • การสร้า งออปเจ็ค ภายในเมธอดที่อ ยู่ใ นคลาสภายนอก สามารถทำา ได้โ ดยตรง  คลาสทีอ ยู่ภ ายในสามารถที่จ ะเรีย กใช้ค ุณ ลัก ษณะ ่
  • 96. ตัว อย่า งโปรแกรมแสดงคลาสที่อ ยู่ ภายในคลาส public class Outer { public class Outer { public void method1() { public void method1() { Inner in1 = new Inner(); Inner in1 = new Inner(); in1.method2(); in1.method2(); }} public class Inner { public class Inner { public void method2() { public void method2() { System.out.println("Inner Demo"); System.out.println("Inner Demo"); }} }} }} public class InnerDemo { public class InnerDemo { public static void main(String args[]) { public static void main(String args[]) { Outer.Inner in2 = new Outer().new Inner(); Outer.Inner in2 = new Outer().new Inner(); in2.method2(); in2.method2(); }} }}
  • 97. คลาสภายในที่อ ยู่ภ ายในเมธอด  คลาสภายในประเภทนี้จ ะมี access modifier เป็น default  เราจะไม่ส ามารถทีจ ะสร้า งออปเจ็ค ของคลาส ่ ภายในประเภทนี้น อกเมธอดทีป ระกาศคลาสได้ ่  คลาสประเภทนีจ ะสามารถเรีย กใช้ต ัว แปรภายใน ้ ของเมธอดได้ใ นกรณีท ต ัว แปรนั้น ประกาศเป็น ี่ final เท่า นัน ส่ว นตัว แปรทีเ ป็น คุณ ลัก ษณะของ ้ ่ คลาสหรือ คุณ ลัก ษณะของออปเจ็ค สามารถทีจ ะ ่ เรีย กใช้ไ ด้เ ช่น เดีย วกับ คลาสภายในทีอ ยูภ ายใน ่ ่ คลาส
  • 98. คุณ สมบัต ิท ส ำา คัญ อื่น ๆของคลาส ี่ ภายใน  คลาสภายในอาจเป็น คลาสแบบ abstract หรือ อิน เตอร์เ ฟสได้  คลาสภายในทีอ ยูภ ายในคลาสภายนอกโดยตรง ่ ่ ถ้า มี modifier เป็น static จะกลายเป็น คลาสปกติ และสามารถสร้า งออปเจ็ค โดยการเรีย กชื่อ ของ คลาสภายนอกได้ด ัง นี้ Outer.Inner in3 = Outer.new Inner();  คลาสภายในไม่ส ามารถทีป ระกาศให้ม ี ่ คุณ ลัก ษณะหรือ เมธอดเป็น แบบ static ได้เ ว้น แต่ คลาสภายในจะเป็น คลาสแบบ static  คลาสภายในสามารถทีจ ะใช้ต ัว แปรทีเ ป็น ่ ่
  • 99. ตัว อย่า งโปรแกรมแสดงคลาสที่อ ยู่ ภายในเมธอด public class MOuter { public class MOuter { private int a = 1; private int a = 1; public void method(final int b) { public void method(final int b) { final int c = 2; final int c = 2; int d = 3; int d = 3; class Inner { class Inner { private void iMethod() { private void iMethod() { System.out.println("a = "+a); System.out.println("a = "+a); System.out.println("b = "+b); System.out.println("b = "+b); System.out.println("c = "+c); System.out.println("c = "+c); System.out.println("d = "+d); System.out.println("d = "+d); //illegal //illegal }} }} }} }}
  • 100. แพคเก็จ  ซอฟต์แ วร์แ พคเก็จ ช่ว ยในการจัด การการพัฒ นา โปรแกรมขนาดใหญ่  ในโปรแกรมภาษาจาวา แพคเก็จ จะเป็น ทีร วมของ ่ คลาสของภาษาจาวาหลายๆคลาส  โปรแกรมอาจแบ่ง เป็น แพคเก็จ และแพคเก็จ ย่อ ย (Subpackage)  แพค เก็จ จะเก็บ ไว้ใ นไดเร็ก ทอรี่ (Directory) ซึ่ง จะเป็น ชื่อ ของแพคเก็จ
  • 102. โครงสร้า งโปรแกรมภาษาจาวา  รูป แบบโปรแกรมภาษาจาวามีด ัง นี้ [<package_declaration>] [<import_declaration>] [<class_declaration>]
  • 103. คำา สัง ่ package  คำา สั่ง package เป็น การระบุว า คลาสอยูใ นแพคเก็จ ่ ่ ใด  รูป แบบของคำา สั่ง package package <package_name>[<sub_package_name>];  ตัว อย่า ง package faculty.domain;  โปรแกรมภาษาจาวาหนึ่ง โปรแกรมจะมีค ำา สัง ่ ได้เ พีย งคำา สั่ง เดีย ว โดยจะเป็น คำา สั่ง แรก package ของโปรแกรม  กรณีท ไ ม่ม ค ำา สั่ง package ี่ ี คลาสจะถูก กำา หนดไว้ใ น แพคเก็จ default
  • 104. คำา สั่ง import  คำา สั่ง import เป็น การเรีย กใช้ค ลาสในแพคเก็จ ต่า งๆ  รูป แบบของคำา สั่ง import import <package_name>[.<sub_package_name>].<Class_name> • หรือ import <package_name>[.<sub_package_name>].*;  ตัว อย่า ง import faculty.reports.Report; • หรือ import java.awt.*;  คำา สั่ง import จะอยูก ่อ นหน้า การประกาศคลาส ่  โปรแกรมภาษาจาวาหนึ่ง โปรแกรมสามารถมีค ำา สั่ง import ได้ห ลายคำา สั่ง
  • 105. สรุป เนื้อ หาของบท  คลาสจะประกอบไปด้ว ยคุณ ลัก ษณะและเมธอด  การเขีย นโปรแกรมเชิง ออปเจ็ค เป็น การแบ่ง โปรแกรมออกเป็น โมดูล ย่อ ยๆ ทำา ให้ส ามารถปรับ เปลี่ย นแก้ไ ขโปรแกรมได้ง ่า ยขึ้น  เมธอดทีก ำา หนดขึ้น ในคลาสใดๆสามารถเรีย กใช้ ่ งานได้ส องรูป แบบคือ การเรีย กใช้ง านจากคลาส ทีต ่า งกัน และการเรีย กใช้ง านภายในคลาส ่ เดีย วกัน  เมธอดทีก ำา หนดขึ้น ในคลาสอาจมี ่ argument ที่ รับ ค่า เพือ นำา ไปใช้ใ นเมธอด ่  เมธอดใดๆของคลาสสามารถทีจ ะมีค ่า ทีส ่ง กลับ มา ่ ่
  • 106. สรุป เนื้อ หาของบท  เมธอดของคลาสใดทีม ี ่ modifier เป็น แบบ static จะทำา ให้ส ามารถถูก เรีย กใช้ง าน โดยใช้ช ื่อ ของ คลาสนัน ได้เ ลย ้  เมธอดแบบ overloaded คือ มีเ มธอดทีม ช ื่อ่ ี เดีย วกัน แต่จ ะมีจ ำา นวนหรือ ชนิด ข้อ มูล ของ argument ทีต ่า งกัน ่  โดยทัว ไป ่ คุณ ลัก ษณะของคลาสจะมี modifier เป็น แบบ private ทัง นีเ พือ ป้อ งกัน การเข้า ถึง จาก ้ ้ ่ คลาสอื่น  โดยทัว ไป ่ เมธอดของคลาสจะมี modifier เป็น แบบ public ทัง นีเ พือ ให้ เมธอดจากคลาสอื่น เรีย ก ้ ้ ่
  • 107. สรุป เนื้อ หาของบท  ชื่อ ของเมธอดทีเ กี่ย วข้อ งกับ คุณ ลัก ษณะของคลาส ่ นิย มใช้ setXxx สำา หรับ เมธอดทีม ไ ว้เ พือ กำา หนด ่ ี ่ ค่า ให้ก ับ คุณ ลัก ษณะ xxx และใช้ getXxx สำา หรับ เมธอดทีจ ะส่ง ค่า กลับ เป็น คุณ ลัก ษณะ xxx ่  ภาษาจาวาได้ก ำา หนดคีย เ วิร ์ด ทีช ื่อ extends ์ ่ เพือ ่ ระบุก ารสืบ ทอดของคลาส  คลาสทุก คลาสในภาษาจาวาถ้า ไม่ไ ด้ส ืบ ทอดจาก คลาสใดเลยจะถือ ว่า คลาสนั้น สืบ ทอดจากคลาสที่ ชื่อ Object  เมธอดแบบoverriden จะเป็น การเขีย นเมธอด ของ superclass ขึ้น ใหม่
  • 108. สรุป เนื้อ หาของบท  Constructor หมายถึง เมธอดทีม ช ื่อ เดีย วกัน กับ ่ ี ชื่อ คลาส แต่จ ะไม่ม ก ารส่ง ค่า กลับ และจะไม่ม ก าร ี ี ใส่ค ีย เ วิร ์ด void โดยคลาสทุก คลาสจะมี default ์ constructor มาให้อ ยูแ ล้ว แต่เ ราสามารถที่จ ะ ่ กำา หนด constructor ขึ้น ใหม่เ องได้  เมธอดทีช ื่อ this() ่ เป็น การเรีย กใช้ constructor ของคลาสตัว เอง  คลาสแบบ final คือ คลาสทีไ ม่ส ามารถสืบ ทอดได้ ่  คลาสแบบ abstract คือ คลาสทีย ง เป็น คลาสทีไ ม่ ่ ั ่ สมบูร ณ์ โดยมีเ มธอดแบบ abstract ซึ่ง เป็น เมธอดที่ ยัง ไม่ส มบูร ณ์อ ย่า งน้อ ยหนึ่ง เมธอด
  • 109. แบบฝึก หัด  แบบฝึก หัด ที่ 1 การเขีย น constructor และ เมธอดแบบ overloaded • จงเขีย นคำา สั่ง ในเมธอดต่า งๆของคลาส Rectangle ให้ สมบูร ณ์  แบบฝึก หัด ที่ 2 การเขีย นโปรแกรมบัญ ชี • จงเขีย นคลาสที่ช ื่อ Account • จงเขีย นคลาสที่ช ื่อ Teller ซึ่ง จะเป็น คลาสที่ใ ช้ใ นการ สร้า งออปเจ็ค ของคลาส Account และทดสอบการเรีย ก ใช้เ มธอดต่า งๆ  แบบฝึก หัด ที่ 3 การเขีย นโปรแกรมบัญ ชีเ ช็ค • จงเขีย นคลาสที่ช ื่อ CheckingAccount • จงแก้ไ ขคลาสที่ช ื่อ TellerGUI โดยใช้ห ลัก การของ Dynamic Binding เพื่อ สร้า ง ออปเจ็ค ของคลาส