Advertisement
Guest User

สรุป java ก่อนสอบ

a guest
Nov 27th, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1.  
  2. 1.) คลาส คือ
  3.  
  4. public class A{
  5.  
  6. }
  7.  
  8. ----------------------------
  9.  
  10. 2.) Fields คือตัวแปรที่ประกาศอยู่บนสุดของ class
  11.  
  12.  
  13. public class A{
  14.     public int    a = 0;
  15.     public String b = 0;
  16. .
  17. .
  18. .
  19. .
  20. }
  21.  
  22. -----------------------------
  23.  
  24. 3.) method คือ
  25.  
  26. public class A{
  27.     public String hello{       // method hello
  28.    
  29.     }
  30. }
  31.  
  32. ----------------------------
  33.  
  34. 4.) overloading เป็นการเขียน method ใหม่ซึ่งมีชื่อซ้ำกัน แต่ประเภทของ parameter ไม่เหมือนกัน
  35.  
  36.  
  37. public class A{
  38.     public int hello(int a){       // method hello
  39.  
  40.     }
  41.     public int hello(String a){     // overloading method hello
  42.  
  43.     }
  44. }
  45.  
  46.  
  47. ----------------------------
  48.  
  49. 5.) constructor คือ method ที่ชื่อเดียวกับคลาส
  50.  
  51. public class A{
  52.     public A(){
  53.  
  54.     }
  55. }
  56.  
  57.  
  58. -----------------------------
  59.  
  60. 6.) การสืบทอดคลาส
  61.  
  62. public class A{
  63. }
  64.  
  65. public class B extends A{
  66. }
  67.  
  68. อธิบาย : คลาส B สืบทอด A
  69.  
  70.  
  71. ---------------------------------
  72.  
  73.  
  74. 7.) การสร้าง Instance  (ไล่ลำดับการทำงานตามตัวเลข)
  75.  
  76.  
  77. public class HelloWorld{
  78.    
  79.     public int a = 0;
  80.  
  81.     public static void main(String[] args){
  82.         HelloWorld h = new HelloWorld();        1) // สร้าง Instance ของคลาส Hello
  83.         h.my_method();                          2) // เรียกใช้งาน my_method ของคลาส HelloWorld
  84.     }
  85.    
  86.     public void my_method(){
  87.        
  88.         int a = 999;                    
  89.        
  90.         System.out.println(a);           3) // print a (ไม่ใช้ this จะได้ 999 จากตัวแปรใน local)
  91.         System.out.println(this.a);      5) // print a (ใช้ this.a จะได้ค่า 0 จาก field ของ class)
  92.     }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement