ต.ย.โปรแกรมเชิงวัตถุ ชุดที่ 1
Home  Contents KMArticlesMembersSponsorsAbout us

ปรับปรุง : 2556-07-08 (ปรับส่วนคำอธิบายใหม่)
OOP :: intro ch1-12 :: keyword & sign :: method calling :: series #1 :: series #2 :: series #3 :: pro_pmy
ขอบซ้ายขอบบน
Encapsulation
1. encapsulation ที่เป็น Composition : แบบ delayed initialization
:: ประกอบด้วย instance 2 ตัว หรือ data members ของ class
:: encapsulation คือการปกป้องข้อมูลในพื้นที่เดียวกัน
:: http://www.narisa.com/forums/index.php?showtopic=1004
    
    class student {
      int id;
      String name;
    }
    class x1 {
      public static void main (String args[]) {
         student x;
         x = new student();
         x.id = 101;
         x.name = "wichep";
         System.out.println(x.id + "  " + x.name);
      }
    }
    
2. encapsulation for 1 object
:: ใช้ method setter และ getter สำหรับเข้าถึง id
:: ใช้ s.id โดยตรงไม่ได้
:: Encapsulation คือ การรวม data และ method ที่เกี่ยวข้องกัน ทำงานร่วมกันเอาไว้ใน object เดียวกัน
แล้วซ่อนรายละเอียดการทำงานของ object และไม่ให้ object อื่นเข้าไปแก้ไข data โดยตรง
ทำให้สะดวกในการแก้ไข code ใน object ที่ encapsulate ไว้ ไม่กระทบ object ที่เรียกใช้
    
    class student {
      private int id;
      void setter(int gid) { id = gid; }
      int getter() { return id; }
    }
    class x {
      public static void main (String args[]) {
        student s = new student();
        s.setter(5601);
        System.out.println(s.getter());
      }
    }
    
3. encapsulation for 1 object
:: ประกาศ reference ในครั้งเดียวด้วย student s1,s2;
:: พื้นที่เก็บข้อมูลไม่ทับซ้อนกัน
:: อ้างอิง object ที่ถูกซ่อนผ่าน this
    
    class student {
      private Integer id;
      void setter(Integer id) { this.id = id; }
      Integer getter() { return this.id; }
    }
    class x {
      public static void main (String args[]) {
        student s1,s2;
        s1 = new student();
        s1.setter(5601);
        s2 = new student();
        s2.setter(5602);
        System.out.println(s1.getter() + "" +  s2.getter());
        // 56015602
      }
    }
    
4. encapsulation and work in private and public
:: work1 ถูกปกป้อง แต่ work2 ไม่ถูกปกป้อง
:: แสดงให้เห็นปัญหา ว่าถ้าปล่อยให้เข้าถึงวัตถุ อาจทำให้ผลลัพธ์เบี่ยงเบนไป กรณีนี้การเข้าถึง work2 จะทำให้ i เปลี่ยน
:: แก้ไขโดยการเปลี่ยนทุก method และ object เป็น private ยกเว้นที่ต้องการ set กับ get เท่านั้น
    
    class student {
      private Integer id;
      void setter(Integer id) { this.id = id; }
      Integer getter() { 
        this.id = work1();
        this.id = work2();
        return this.id; 
      }
      private Integer work1() { 
        this.id++; 
        return this.id;
      }  
      public Integer work2() { 
        this.id++; 
        return this.id;
      }  
    }
    class x {
      public static void main (String args[]) {
        student s;
        s = new student();
        s.setter(01);
        System.out.println(s.work2()); // = 2 
        System.out.println(s.getter()); //  = 4 
      }
    }
    
ขอบซ้ายขอบบน
Polymorphism
5. Upcasting ใน การพ้องรูป(Polymorphism)
:: ตัวอย่างนี้ แสดงให้เห็นว่าหลักการของ Upcasting สามารถเรียก constructor ของ class แม่มาทำงาน
:: สร้าง object ด้วย a xxx1 = new a1(); เพื่อเรียก constructor ลูกของ a
:: เมื่อสั่งงาน xxx1.anymethod(); ก็จะควานหาจากใน a1 มิใช่ a
    // x.java : เรียก a1 ซึ่งเป็นลูกของ a มาทำงาน 
    class a { 
      a() { 
        System.out.println("a"); 
      } 
      static void a22() { 
        System.out.println("oh no"); 
      } 
    } 
    class a1 extends a { 
      a1() { 
        System.out.println("a1"); 
      } 
    } 
    class a2 extends a { 
      a2() { 
        System.out.println("a2"); 
      } 
      static void a22() { 
        System.out.println("a22"); 
      } 
    } 
    public class x { 
      public static void main (String args[]) { 
        a xxx1 = new a1(); 
        a xxx2; 
        xxx2 = new a2(); 
        a2 xxx3 = new a2(); 
        xxx3.a22(); 
      } 
    } 
    // Result of this program 
    // a 
    // a1 
    // a 
    // a2 
    // a 
    // a2 
    // a22
    
ขอบซ้ายขอบบน
this
6. this and overload of this
:: function this ต้องอยู่บรรทัดแรกของ constructor
    
    class child {
      child() { 
        this(5); // this() must be first line under constructor
        System.out.println(this.i);
      }
      child(int i) { System.out.println(i); }
      static int i = 1;
      public static void main(String args[]) {
        new child();
      }
    }
    
7. this references
:: ไม่สามารถเรียก this ใน main ดังนั้นโปรแกรมนี้ compile ไม่ผ่าน
:: การเรียกใช้ this reference แบบนี้ไม่ได้
:: Java กำหนดให้เรียก this ใน constructor เท่านั้น
:: Java กำหนดให้เรียก this ใน constructor ได้เพียงครั้งเดียว และต้องอยู่บรรทัดแรกใต้ constructor
    
    class x1 {
      // this program error on compile
      // error on theory of this
      x1() { System.out.print("ketsarin"); }
      public static void main (String args[]) {
        this();
      }
    }
    
8. this references
:: แสดงการใช้ constructor แบบ overloading
:: this จะหมายถึง class ปัจจุบัน เมื่อถูกเรียกต้องอยู่บรรทัดแรก
:: ถ้า x1 เรียก x2 แบบไม่ส่งค่า จากนั้นจะเรียก x2 แบบส่งค่าใหม่
    
    class x2 {
      x2() { this('a'); } // แต่ใช้ x2('a') ไม่ได้
      x2(char y) { System.out.println(y); }
    }
    class x1 {
      public static void main (String args[]) {
        new x2();
      }
    }// output : a
    
9. การใช้ this ระหว่าง constructor กับ static
:: กรณีนี้ ใน main ทำงานตามด้วย instance และ constructor
    
    class father {
      static int box1 = 5; // required static in main
    }
    // ===
    class child extends father {
      child() {
        int box2 = this.box2 + box1 + 1;
        System.out.println(this.box2 + box2); // 18
      }
      static int box2 = box1 + 1;
      public static void main(String args[]) {
        new child();
      }
    }
    
ขอบซ้ายขอบบน
Abstract
10. abstract class หมายถึง class ที่ไม่ระบุรายละเอียดการทำงาน
abstract class มีขึ้นเพื่อ class หรือ method ขึ้นมา แต่ยังไม่ระบุการทำงาน การทำงานจะรอให้ class ลูก overriding ระบุการทำงานในภายหลัง
ถ้าใน class มี method ที่เป็น abstract ตัว class ต้องเป็น abstract class ด้วย หลักการของ abstract ทำให้ลดความซ้ำซ้อนของโปรแกรม และเห็นหน้าที่ของแต่ละ method ได้ชัดเจน แยกหน้าที่การเป็น interface หรือ implementation ได้ชัดเจน
เมื่อประกาศ workinfile ให้มีการทำงาน 2 method ที่เป็น abstract เมื่อ w รับการสืบทอดไป จะต้องเขียนรายละเอียดการทำงานให้ครบ ในทุก method ที่เป็น abstract มิเช่นนั้น class ที่รับการสืบทอด จำเป็นต้องเป็น abstract ด้วย และถ้า class เป็น abstract ก็จะไม่สามารถ new หรือสร้างวัตถเพื่อทำงานได้
    
    // x.java
    abstract class workinfile { 
      public abstract void edit(); 
      public abstract void show(); 
      public void display(){ 
        System.out.println("display"); 
      } 
    } 
    class w extends workinfile { 
      public void edit(){ 
        System.out.println("edit"); 
      } 
      public void show(){ 
        System.out.println("show"); 
      } 
    } 
    public class x { 
      public static void main (String args[]) { 
        w aaa = new w(); 
        aaa.edit(); 
        aaa.show(); 
        aaa.display(); 
      } 
    } 
    // Result of this program
    // edit 
    // show
    // display
    
ขอบซ้ายขอบบน
Interface
11. interface หมายถึง abstract class ที่มีทุก method เป็น abstract object
การเป็น interface class หมายถึงเป็น class ที่เตรียมไว้ให้ class อื่น ๆ เข้ามาติดต่อ โดยปกติการสืบทอดจะใช้ extends แต่ class แบบ interface จะใช้คำว่า implements แทน
    
    interface a {
      int bb = 10;
      public void show();
      public int i();
    }
    class c implements a {
      public void show() {
        System.out.println("show"); 
      }
      public int i() { return (bb + 5); }
    }
    public class x { 
      public static void main (String args[]) { 
        c aaa = new c(); 
        aaa.show(); 
        System.out.println(aaa.i()); 
      } 
    } 
    // Result of this program
    // show
    // 15
    
ขอบซ้ายขอบบน
package + abstact + interface
12. package + abstact + interface
    
    package a;
    public class b{
      public void b1() {
        System.out.println("1");
      }
    }
    
    abstract class d {
      void e () {
        System.out.println("2");
      }
      abstract void f ();
    }
    
    interface g {
      int h = 5;
      public void i ();
    }
    
    interface j {
      void k ();
    }
    
    import a.*;
    public class l extends d implements g,j{
      public static void main(String a[]){
        new b().b1();
        l x = new l();
        x.e();
        x.f();
        x.i();
        x.k();
        System.out.println(x.h);
      }
      public void f () {System.out.println("f");}
      public void i () {System.out.println("i");}
      public void k () {System.out.println("k");}
    }
    
ขอบซ้ายขอบบน
Thread
13. Thread isAlive
:: โปรแกรมแสดงผลแล้วหยุด 1 วินาที ก่อนแสดงบรรทัดถัดไป
:: Thread.yield() คือ หยุดทำงานชั่วคราว และใช้ Thread.start() ปลุกให้ตื่นขึ้นมา หรือมีชีวิตอีกครั้ง
:: Thread.stop, Thread.suspend and Thread.resume is deprecated (ถูกยกเลิก)
:: http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html
    
    class x {
    static Thread t1 = Thread.currentThread();
    public static void main(String args[]) {
      Thread t0 = new Thread("t0");          // t0
      newthread(t0);
      sleep1();
      System.out.println(t1.getName());      // main (default name)
      sleep1();
      System.out.println(t1.getPriority());  // 5
      sleep1();
      System.out.println(t1.isAlive());      // true
      newthread(t1);                         // main
      t0.yield(); 
      System.out.println(t0.isAlive());      // false
      t0.start(); 
      System.out.println(t0.isAlive());      // true
    }
    static void newthread(Thread t) {
      System.out.println(t.getName());
    }
    static void sleep1 () {
      try { Thread.sleep(1000); } catch (InterruptedException e) { }
    }
    }
    // t0
    // main
    // 5
    // true
    // main
    // false
    // true
    
14. Thread แบบ extends Thread
:: start() หมายถึงการเริ่มต้น แต่ run() จะมี block สำหรับดำเนินงาน
:: mthread(String n) คือ overload of constructor
:: แยก Thread ตั้งแต่ตอนที่เรียก เพราะเรียก class ที่ extends Thread
:: ใช้ implements Runnable ไม่ได้ กับ mthread เพราะมีการใช้ super ซึ่งต้องเป็นเทรด
:: ผลลัพธ์ที่ได้ไม่แน่นอน ขึ้นกับว่าเทรดไหนจะแย่งเข้าหน่วยประมวลผลได้อย่างไร
    
    class x {
    public static void main(String args[]) {
      new mthread("a").start();
      new mthread("b").start();
    }
    } 
    // ===
    class mthread extends Thread {
      // super ก็คือ Thread เพราะ mthread มีการ extends จาก Thread
      mthread(String n) { super(n); } // ค่า n กลายเป็นชื่อของ Thread
      public void run() {
        for(int i=0;i<200;i++) System.out.print(getName());
      }
    } 
    // random : aaabaaabaabbbabbabbbbabbbbbbbaaabbbbbbbaba
    
15. Thread
:: คลาส mthread จะใช้แบบ implements Runnable หรือ extends Thread ก็ได้
:: แยก Thread ตั้งแต่ตอนที่เรียก เพราะเรียก class ที่ extends Thread
    
    class x {
    public static void main(String args[]) {
      new mthread("a").t.start();
      new mthread("b").t.start();
    }
    } 
    // ===
    // class mthread extends Thread { // both is ok
    class mthread implements Runnable {
      Thread t;
      mthread(String n) { t = new Thread(this,n); }
      public void run() {
        for(int i=0;i<200;i++) 
           System.out.print(t.getName());
      }
    } 
    // random : aaabaaabaabbbabbabbbbabbbbbbbaaabbbbbbbaba
    
ขอบซ้ายขอบบน
runtime
16. Runtime Class และ method #1
:: จากการทดสอบบน 1.7.0_03 พบว่าผลต่างออกไปจาก 1.4 คือ ตัวเลขเกือบคงที่ยกเว้น รายการที่ 3
:: บางครั้งการสั่ง new Integer[10] ก็ไม่ใช้พื้นที่
:: ตัวเลขในรายการที่ 4 และ 5 เท่ากัน แสดงว่ารุ่น 1.7.0_03 การให้ค่า null ไม่มีผลให้ลดขนาดใน heap

class x{
  public static void main(String args[]) {
    Runtime r = Runtime.getRuntime(); 
    System.out.println("Total memory is: " + r.totalMemory()); 
    System.out.println("1 Free memory: " + r.freeMemory()); 
    r.gc(); // clear bin
    System.out.println("2 Free memory: " + r.freeMemory()); 
    Integer someints[] = new Integer[10]; 
    System.out.println("3 Free memory: " + r.freeMemory()); 
    for(int i=0; i<10; i++) {
      someints[i] = new Integer(i); // allocate integers 
    }
    System.out.println("4 Free memory: " + r.freeMemory()); 
    for(int i=0; i<10; i++) someints[i] = null; 
    System.out.println("5 Free memory: " + r.freeMemory());
    r.gc(); // clear bin 
    System.out.println("6 Free memory: " + r.freeMemory());
  }
}
ผลการทำงานของรุ่น 1.4
// Total memory is: 2031616
// คงถูกใช้โดยโปรแกรมไปแล้ว แสนกว่าไบท์
// 1 Free memory: 1916848 (- 114768) 
// เก็บกวาด แล้วดูขนาดใหม่ มีพื้นที่เพิ่ม 30072 bytes  แสดงว่าการเก็บกวาดได้ผล
// 2 Free memory: 1946920 (+ 30072)
// จองใน heap ให้อาร์เรย์ของ Integer 10 ตัว ใช้พื้นที่ไป 616 bytes
// 3 Free memory: 1946304 (- 616) 
// สั่งครอบครองพื้นที่ใน heap และกำหนดค่าลงในแต่ละสมาชิก ใช้พื้นที่ไป 432 bytes
// 4 Free memory: 1945872 (- 432) 
// สมาชิกที่เคยกำหนดค่าไป เปลี่ยนเป็น null ทั้งหมด ได้พื้นที่กลับมา 272 bytes
// 5 Free memory: 1945600 (+ 272)
// เก็บกวาด มีพื้นที่เพิ่ม 1264 bytes
// 6 Free memory: 1946864 (+ 1264)

ผลการทำงานของรุ่น 1.7.0_03
Total memory is: 15204352
1 Free memory: 14834648 (- 369704)
2 Free memory: 14977088 (+ 142440) แสดงว่าเก็บกวาดแล้วได้พื้นที่เพิ่มแสนกว่า
3 Free memory: 14891784 (- 85304) ใช้พื้นที่สำหรับอาร์เรย์ 10 ตัว แปดหมื่นกว่า
4 Free memory: 14891784
5 Free memory: 14891784
6 Free memory: 14976200 (+ 84416) ล้างถังขยะแล้วได้พื้นที่กลับมา แปดหมื่นกว่า
17. Runtime Class และ method #2
:: จากการประมวลผลติดต่อกันหลายครั้ง พบว่าเลขที่ได้เท่ากัน แม้เปิดใน cmd คนละหน้าต่าง ผลก็เหมือนกัน
:: http://www.java-samples.com/showtutorial.php?tutorialid=231

class x{
  public static void main(String args[]) {
    Runtime r = Runtime.getRuntime(); 
    long mem1, mem2; 
    Integer someints[] = new Integer[1000]; 
    System.out.println("Total memory is: " + r.totalMemory()); 
    mem1 = r.freeMemory(); 
    System.out.println("Initial free memory: " + mem1); 
    r.gc(); 
    mem1 = r.freeMemory(); 
    System.out.println("Free memory after gc: " + mem1); 
    for(int i=0; i<1000; i++) {
      someints[i] = new Integer(i); // allocate integers 
    }
    mem2 = r.freeMemory(); 
    System.out.println("Free memory after allocation: " + mem2); 
    System.out.println("Memory used by allocation: " + (mem1-mem2)); 
    // discard Integers 
    for(int i=0; i<1000; i++) someints[i] = null; 
    r.gc(); // request garbage collection 
    mem2 = r.freeMemory(); 
    System.out.println("Free memory after gc: " + mem2);
  }
}

ผลการทำงานของรุ่น 1.7.0_03
Total memory is: 15204352
Initial free memory: 14834592 (- 369760)
Free memory after gc: 15057968 (+ 223376)
Free memory after allocation: 14887400 (- 170568)
Memory used by allocation: 170568
Free memory after gc: 15057560 (+ 170160)
ขอบซ้ายขอบบน
Appendix
18. การจัดการค่าที่อยู่ในคลาสภายนอก
:: instance ของ atichart ถูกสร้าง และก็ถูกแทนที่ ไม่สามารถนำมาใช้ได้อีก
:: instance ของ sasivimon ถูกเรียกใช้ครั้งเดียว
    
    class student {
      student(String x) {
        System.out.println(x);
        n = x;
      }
      String n;
    }
    class x1 {
      public static void main (String args[]) {
        student s = new student("atichart");
        System.out.println(s.n);
        s = new student("sasivimon");
        s = new student("wichep");
        System.out.println(s.n);
      }
    }
    // atichart
    // atichart
    // sasivimon
    // wichep
    // wichep
    
19. break, continue, return
:: คำสั่งที่ใช้คุมการดำเนินการในกรณีพิเศษ
:: break ใช้หยุดขณะอยู่ใน block หรือออกจาก block ตัวล่าสุด
:: continue ใช้กลับไปยังต้น block ใช้ข้ามการทำงานครั้งนั้น แต่ไม่หลุดจาก loop
:: return ถ้าคืนค่าต้องอยู่บรรทัดสุดท้าย และต้องทำงานใน method
    
    class x1 {
      public static void main (String args[]) {
      b: for (int i=0;i<5;i++) {
           if (i == 2) break b; else System.out.print(i);
    	 }
    // 01   
         for (int i=0;i<5;i++) {
           if (i == 2) continue; else System.out.print(i);
         }
    // 0134
         x2();
    // 01
         System.out.print(8);
         System.out.print(x3());
    // 876
      }
      public static void x2 () {
        for (int i=0;i<5;i++) {
          if (i == 2) return; else System.out.print(i);
        }    
        System.out.print(10);
      }
      public static int x3 () {
        System.out.print(7);
    	return (6);
      }
    } 
    // 01013401876
    
"Imagination is more important than knowledge" - Albert Einstein
Home
Thaiabc.com
Thainame.net
Lampang.net
Nation university
PHP
MySQL
Visual basic.NET
TabletPC
Linux
Online quiz
Download
Search engine
Web ranking
Add website
Blog : Education
Blog : ACLA
Blog : Lampang
Facebook.com
Twitter.com
About us
My dream
Site map
Sponsor
http://goo.gl/72BPC