30/09/2018, 21:07

Cho em hỏi về lệnh copy Constructor Java với

class Student6{  
    int id;  
    String name;  
    Student6(int i,String n){  
    id = i;  
    name = n;  
    }  
      
    Student6(Student6 s){  
    id = s.id;  
    name =s.name;  
    }  
    void display(){System.out.println(id+" "+name);}  
   
    public static void main(String args[]){  
    Student6 s1 = new Student6(111,"Hoang");  
    Student6 s2 = new Student6(s1);  
    s1.display();  
    s2.display();  
   }  
}   

Em mới tập tành học Java thôi anh chị cho em hỏi là dòng Student6(Student6 s) là thế nào ạ, trong Constructor chỉ truyền đối số thôi vậy Student6 s là thế nào ai giúp đỡ em về kiến thức này với.

cescnghia viết 23:08 ngày 30/09/2018
Student A = new Student(15, "A"); => tạo ra 1 sinh viên A với id = 15.

Student B = new Student(A) => Bạn tạo ra 1 sinh viên giống như sinh viên A.
Qu Thành viết 23:17 ngày 30/09/2018

Cho mình cảm ơn nhé

Bài liên quan
0