| ต.ย.โปรแกรมเชิงวัตถุ ชุดที่ 1 | 
| Home | Contents | KM | Articles | Members | Sponsors | About us | 
| ปรับปรุง : 2556-09-25 (horse) | 
| OOP :: intro ch1-12 :: keyword & sign :: method calling :: series #1 :: series #2 :: series #3 :: pro_pmy | 
|  |  | |
| Encapsulation | ||
| 
 
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);
  }
}
 | |
| 
 
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());
  }
}
 | |
| 
 
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
  }
}
 | |
| 
 
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 | ||
| 
 // 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 | ||
| 
 
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();
  }
}
 | |
| 
 
class x1 {
  // this program error on compile
  // error on theory of this
  x1() { System.out.print("ketsarin"); }
  public static void main (String args[]) {
    this();
  }
}
 | |
| 
 
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
 | |
| 
 
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 | ||
| 
 
abstract class x { 
  public abstract void show(); 
  public static void main (String args[]) { 
    person p[] = new person[3];
    p[0] = new person("tom");
    p[1] = new person("boy");
	System.out.println(p[0].getName());
	System.out.println(p[1].getName());
  } 
} 
class person {
  private String myname;
  person(String n) {
    myname = n;
  }
  String getName() { return myname; }
}
 | |
| 
 
import java.util.*;
abstract class y { 
  public abstract void show(); 
  TreeSet ss = new TreeSet(); // SortedSet
} 
//===
class x extends y { 
  public void show() {
	System.out.println(ss.size()); // 3
    ss.remove("tom");
	System.out.println(ss.contains("tom")); // false
	System.out.println(ss.size()); // 2
	System.out.println(ss); 
    Iterator it = ss.iterator(); // ใช้สำหรับเข้าถึง element
    while (it.hasNext()) {
       Object element = it.next();
       System.out.println(element.toString());
    } // ant boy
  }	  
  public static void main (String args[]) { 
    x o = new x();
	o.ss.add("tom");
    o.ss.add("boy");
    o.ss.add("ant");
    o.show();
  } 
} 
 | |
| 
 
// 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 | ||
| 
 
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 | ||
| 
 
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 | ||
| 
 
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
 | |
| 
 
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
 | |
| 
 
class CallThread {
public static void main(String args[]) {
  new t1().start();
  new t2().start();
  new t1().start();
  new t2().start();
}
} 
// ===
class t1 extends Thread {
  public void run() {
    for(int i=0;i<10;i++) {
      System.out.print("1");
      try { Thread.sleep(10); } catch (InterruptedException e) { }
    }
  }
} 
// ===
class t2 extends Thread {
  public void run() {
    for(int i=0;i<10;i++) {
      System.out.print("2");
      try { Thread.sleep(10); } catch (InterruptedException e) { }
    }
  }
} 
 | |
| 
 
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
 | |
| 
 
class HorseStart {
public static void main(String args[]) {
  new Horse1().start();
  new Horse2().start();
}
}
// ===
class Horse1 extends Thread {
public void run() {
  for(int i=0;i<3;i++) {
    try { 
      Thread.sleep((int)(Math.random() * 1000)); 
    } catch (InterruptedException e) { }
  }
  CheckFinish chk = new CheckFinish();
  if (chk.flag++ == 1) System.out.println("1");
}
}
// ===
class Horse2 extends Thread {
public void run() {
  for(int i=0;i<3;i++) {
    try { 
      Thread.sleep((int)(Math.random() * 1000)); 
    } catch (InterruptedException e) { }
  }
  CheckFinish chk = new CheckFinish();
  if (chk.flag++ == 1) System.out.println("2");
}
}
// ===
class CheckFinish {
  static int flag = 0;
}
 | ||||
| 
 
class MyHorseStart {
public static void main(String args[]) {
  new MyHorse("tom").start();
  new MyHorse("jack").start();
  new MyHorse("coco").start();
}
}
// ===
class MyHorse extends Thread {
static int flag = 0;
String horse_id;
MyHorse(String horse_id) {
  this.horse_id = horse_id;
}
public void run() {
  try { 
    int r = (int)(Math.random() * 1000);
    Thread.sleep(r); 
  } catch (InterruptedException e) { }
  if (flag++ == 1) System.out.println(horse_id);
}
}
 | ||||
|  |  | |
| runtime | ||
| 
 
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());
  }
}
 | ||
| 
 
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 | ||
| 
 
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
 | |
| 
 
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 |