Advertisement
Guest User

Untitled

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