THÂN CHÀO QUÝ BẠN
Blogger nầy chỉ tồn trử kiến thức và kinh nghiệm về nghề “Materials Testing”, “Textile Screen Printing” và “Internet Programming” của một kỹ sư đã phục vụ tại :
* Trung Tâm Khảo sát Kỹ Thuật Quân Nhu /QLVNCH “ & “ Viện Quốc Gia Định Chuẩn/VNCH”
* Xí nghiệp “Hiệp Hưng / VN” ( sãn xuất giày vãi cao su )
"Bradbury Company Inc/USA"(Textile Screenprinting,Imprinted Sportswear Programs) & “Sundance Graphics Inc / USA” ( in bông vãi sợi ) từ năm 1965 để dành cho thế hệ trẻ muốn học nghề.
Vạn Vật Thái Bình (PEACE ON EARTH).

Saturday, June 13, 2015

JAVA CONSTRUCTOR

JAVA CONSTRUCTORS-HOW TO USE THEM?
 GHI CHÚ HƯỚNG DẪN.
Constructor là một  “block of code”  có mục đích dành chỗ trong memory cho objects của Java class.
Constructor có ký hiệu viết giống hệt method ( còn gọi function ) nhưng không phải method vì không có return type.Còn method thì có return type.
Thí du đây là những constructors có syntax như sau : Rextester(){….  }; Baxao(){….  } ; Xaoke(){….  }; Box ();{….  } Car(){….  }; Dog(){….  } .
Nếu trong round brackets (… ) bỏ trống thì gọi default constructor.
Trong curly brackets{….  } là chỗ chúng ta muốn viết những gì để in ra .
Muốn vận hành constructor thì phải taọ một object cho class.
Thí dụ chúng ta chọn tên class Rextester thì object của class là : Rextester obj = new Rextester();
Nếu constructor có arguments thì trong object phải chứa variables mà chúng ta muốn pass tới arguments.
Thí dụ đây là constructor có 3 arguments chữ màu xanh lá cây : Rextester(int id1, int id2, String str).
Đây là object có chứa 3 variables chữ màu pink : Rextester obj3 = new Rextester(1967, 2015 , " Hello Friends ! VQGĐC still survives but its name was changed after 1975" );
Vì constructor không phải là function hay method nên chúng ta không thể call nó trực tiếp.
Muốn constructor được vận hành(executed), chúng ta phải viết một method khác, thí dụ public void display().Trong method nầy chúng ta viết những gì đã viết trong constructor và ra lệnh cho display() in ra những thứ đó khi chúng ta call nó bằng cách viết như sau :                  obj3.display();
Muốn có obj3 , chúng ta phải taọ một object thí dụ tên là new Rextester(…….) mà trong round brackets(…….) có chứa những variables chúng ta muốn pass vào contructor.
Nếu trong constructor có 3 arguments thì chúng ta phải có đủ 3 variables để pass vào constructor.
Thí dụ đây là object obj3 có đủ 3 variables.
      Rextester obj3 = new Rextester(1967, 2015 , " Hello Friends ! Viên Quôc Gia Đinh Chuân still survives at old location in Biên Hoa but its name was changed after 1975.\nDO YOU KNOW WHY???" );      
 
                           -----------------------
   Thí dụ  Java constructors và cách xử dụng.
   
1-  CONSTRUCTOR CÓ DÙNG KEYWORD “THIS”.
  Chữ this dùng để   “refer to the object in the constructor ”.
 
class Rextester{
  int id1, id2;
  String str;
    //Đây là default constructor nên không có parameters
   Rextester(){
      System.out.println("Default constructor does’t have parameters.");}
    
   // Đây là parameterized constructor có 3 args.  
  public Rextester(int id1, int id2, String str){
       this.id1=id1; this.id2=id2; this.str=str;
      System.out.println("Parameterized constructor having 3 args.\n"); }
      
    // Đây là method để display3 args.
  public void display(){
  System.out.println( id1 + " " + id2 + " " + str);}
 
 public static void main(String args[]){
      
      //This invokes default constructor
      Rextester obj = new Rextester();
      
      // This  invokes the constructor of 3 args  
      Rextester obj3 = new Rextester(1967, 2015 , " Hello Friends ! Viên Quôc Gia Đinh Chuân still survives at old location in Biên Hoa but its name was changed after 1975.\nDO YOU KNOW WHY???" );      
                  obj3.display(); // calls the above function display
}
}
output.
Compilation time: 0.73 sec, absolute running time: 0.14 sec, 
cpu time: 0.07 sec, memory peak: 23 Mb, absolute service time: 0.88 sec
 
Default constructor does’t have parameters.
Parameterized constructor having 3 args.
 
1967 2015  Hello Friends ! Viên Quôc Gia Đinh Chuân still survives at old location in Biên Hoa but its name was changed after 1975.
DO YOU KNOW WHY???
                     Permanent link: http://rextester.com/JWTM8681
 
2- CONSTRUCTOR KHÔNG  DÙNG KEYWORD “THIS”.
 
class Rextester {
  private String myVar;
  public Rextester(String str) {
      myVar = str;  } //Constructor having no keyword this.
  public String getString(){ //Đây là method có return type là String
      return myVar;}
 
  public static void main(String[] args) {
    Rextester ct = new Rextester("Hello Friends ! Thân chào quý ban"); 
    System.out.println(ct.getString());
  }
}
output
 
Hello Friends ! Thân chào quý ban
 
3- METHOD HAY FUNCTION CÓ RETURN TYPE LÀ STRING.
 
Ghi chú hướng dẫn.
Khi viết method hay function muốn có return type thi method phải có chữ String nếu muốn String type, phải có chữ int nếu muốn integer type.
Method phải có chữ static nếu muốn call function bằng cách viết display( ) trong main method.
Nếu không có chữ static thì sẽ xuất hiện error như đây.
“error: non-static method display(String) cannot be referenced from a static context”
Nếu không dùng chữ static thì phải tạo một object cho class rồi call method bằng cách nối object với display( ) trong main method như thí dụ trên.
                                                ----------------------
class Rextester {
  private String str;  // không cần
  public static String display( String str){
      return str ;// Đây là method có return type là String.
  public static void main(String[] args) {
    System.out.println(display("Hello Friends ! Thân chào quý ban") ); 
  }
}
output
Compilation time: 0.82 sec, absolute running time: 0.14 sec,
 cpu time: 0.08 sec, memory peak: 23 Mb, absolute service time: 0.97 sec
 
Hello Friends ! Thân chào quý ban