មេរៀនទី១២: Constructor ក្នុង Java
Constructor ជាប្រភេទពិសេសនៃ method ដែលត្រូវបានប្រើសម្រាប់ចាប់ផ្តើម object។
តួនាទីសម្រាប់បង្កើត constructor
មានមូលដ្ឋានពីរក្នុងការបង្កើត constructor.
1. ឈ្មោះ Constructor name ត្រូវដូចឈ្មោះ class name
2. Constructor មិនមាន return type
បណ្តាប្រភេទនៃ constructors
មានពីរប្រភេទ constructors:
1. default constructor (no-arg constructor)
2. ប៉ារ៉ាម៉ែត្រ constructor
1) Default Constructor
A constructor that have no parameter is known as default constructor.
Syntax of default constructor:
1.(){}
ឧទាហរណ៍ default constructor
យើងបង្កើត no-arg constructor ក្នុង Bike class
class Bike1{
Bike(){System.out.println(“Bike is created”);}
public static void main(String args[]){
Bike1 b=new Bike1();
}
}
Output: Bike is created
ប្រសិនបើមិនមាន constructor ក្នុង class, compiler ដោយស្វ័យប្រវត្តជា default constructor។
ឧទាហរណ៍នៃ default constructor ដែលបង្ហាញតម្លៃ default
class Student3{
int id;
String name;
void display(){System.out.println(id+” “+name);}
public static void main(String args[]){
Student3 s1=new Student3();
Student3 s2=new Student3();
s1.display();
s2.display();
}
}
Output:0 null
0 null
ឧទាហរណ៍នៃ parameterized constructor
យើងបានបង្កើត constructor នៃ Student class ដែលមានពីរប៉ារ៉ាម៉ែត្រ
class Student4{
int id;
String name;
Student4(int i,String n){
id = i;
name = n;
}
void display(){System.out.println(id+” “+name);}
public static void main(String args[]){
Student4 s1 = new Student4(111,”Karan”);
Student4 s2 = new Student4(222,”Aryan”);
s1.display();
s2.display();
}
}
Output:111 Karan
222 Aryan
Constructor Overloading
Constructor overloading គឺជាបច្ចេកទេសមួយក្នុង Java ក្នុង class មួយអាចមានចំនួនច្រើន នៃ constructors ក្នុង parameter lists។
ឧទាហរណ៍នៃ Constructor Overloading
class Student5{
int id;
String name;
int age;
Student5(int i,String n){
id = i;
name = n;
}
Student5(int i,String n,int a){
id = i;
name = n;
age=a;
}
void display(){System.out.println(id+” “+name+” “+age);}
public static void main(String args[]){
Student5 s1 = new Student5(111,”Karan”);
Student5 s2 = new Student5(222,”Aryan”,25);
s1.display();
s2.display();
}
}
Output:111 Karan 0
222 Aryan 25
ភាពខុសគ្នារវាង constructor និង method ?
ចម្លងតម្លៃនៃ object ទៅ constructor in C++
មានវិធីច្រើនដើម្បី copy តម្លៃនៃ object ក្នុងផ្សេងទៀត
• ដោយ constructor
• ដោយផ្តល់តម្លៃនៃ object ក្នុងផ្សេងទៀត
• ដោយ clone() method នៃ Object class
ឧទាហរណ៍.
class Student6{
int id;
String name;
Student6(int i,String n){
id = i;
name = n;
}
Student6(Student 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,”Karan”);
Student6 s2 = new Student6(s1);
s1.display();
s2.display();
}
}
Output:111 Karan
111 Karan
ចម្លងតម្លសនៃ object ទៅផ្សេងទៀតដោយមិនប្រើ constructor
class Student7{
int id;
String name;
Student7(int i,String n){
id = i;
name = n;
}
Student7(){}
void display(){System.out.println(id+” “+name);}
public static void main(String args[]){
Student7 s1 = new Student7(111,”Karan”);
Student7 s2 = new Student7();
s2.id=s1.id;
s2.name=s1.name;
s1.display();
s2.display();
}
}
Output:111 Karan
តួនាទីសម្រាប់បង្កើត constructor
មានមូលដ្ឋានពីរក្នុងការបង្កើត constructor.
1. ឈ្មោះ Constructor name ត្រូវដូចឈ្មោះ class name
2. Constructor មិនមាន return type
បណ្តាប្រភេទនៃ constructors
មានពីរប្រភេទ constructors:
1. default constructor (no-arg constructor)
2. ប៉ារ៉ាម៉ែត្រ constructor

A constructor that have no parameter is known as default constructor.
Syntax of default constructor:
1.
ឧទាហរណ៍ default constructor
យើងបង្កើត no-arg constructor ក្នុង Bike class
class Bike1{
Bike(){System.out.println(“Bike is created”);}
public static void main(String args[]){
Bike1 b=new Bike1();
}
}
Output: Bike is created
ប្រសិនបើមិនមាន constructor ក្នុង class, compiler ដោយស្វ័យប្រវត្តជា default constructor។

class Student3{
int id;
String name;
void display(){System.out.println(id+” “+name);}
public static void main(String args[]){
Student3 s1=new Student3();
Student3 s2=new Student3();
s1.display();
s2.display();
}
}
Output:0 null
0 null
ឧទាហរណ៍នៃ parameterized constructor
យើងបានបង្កើត constructor នៃ Student class ដែលមានពីរប៉ារ៉ាម៉ែត្រ
class Student4{
int id;
String name;
Student4(int i,String n){
id = i;
name = n;
}
void display(){System.out.println(id+” “+name);}
public static void main(String args[]){
Student4 s1 = new Student4(111,”Karan”);
Student4 s2 = new Student4(222,”Aryan”);
s1.display();
s2.display();
}
}
Output:111 Karan
222 Aryan
Constructor Overloading
Constructor overloading គឺជាបច្ចេកទេសមួយក្នុង Java ក្នុង class មួយអាចមានចំនួនច្រើន នៃ constructors ក្នុង parameter lists។
ឧទាហរណ៍នៃ Constructor Overloading
class Student5{
int id;
String name;
int age;
Student5(int i,String n){
id = i;
name = n;
}
Student5(int i,String n,int a){
id = i;
name = n;
age=a;
}
void display(){System.out.println(id+” “+name+” “+age);}
public static void main(String args[]){
Student5 s1 = new Student5(111,”Karan”);
Student5 s2 = new Student5(222,”Aryan”,25);
s1.display();
s2.display();
}
}
Output:111 Karan 0
222 Aryan 25
ភាពខុសគ្នារវាង constructor និង method ?
Constructor
|
Method
|
Constructor គឺត្រូវបានប្រើពេលចាប់ផ្តើមនៃ object. | Method ត្រូវបានប្រើដើម្បីថ្លែងការរបស់ object។ |
Constructor មិនមាន return type. | Method ត្រូវមាន return type. |
Constructor ត្រូវបានបញ្ចេញក្រៅ | Method ចូលពីក្រៅ |
java compiler ផ្តល់ចា default constructor ប្រសិនបើ មិនមាន constructor ផ្សេងទៀត។ | Method មិនបានផ្តល់ដោយ compiler |
ឈ្មោះ Constructor name ត្រូវដូច class name. | ឈ្មោះ Method name មិនអាចដូច class name. |
មានវិធីច្រើនដើម្បី copy តម្លៃនៃ object ក្នុងផ្សេងទៀត
• ដោយ constructor
• ដោយផ្តល់តម្លៃនៃ object ក្នុងផ្សេងទៀត
• ដោយ clone() method នៃ Object class
ឧទាហរណ៍.
class Student6{
int id;
String name;
Student6(int i,String n){
id = i;
name = n;
}
Student6(Student 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,”Karan”);
Student6 s2 = new Student6(s1);
s1.display();
s2.display();
}
}
Output:111 Karan
111 Karan
ចម្លងតម្លសនៃ object ទៅផ្សេងទៀតដោយមិនប្រើ constructor
class Student7{
int id;
String name;
Student7(int i,String n){
id = i;
name = n;
}
Student7(){}
void display(){System.out.println(id+” “+name);}
public static void main(String args[]){
Student7 s1 = new Student7(111,”Karan”);
Student7 s2 = new Student7();
s2.id=s1.id;
s2.name=s1.name;
s1.display();
s2.display();
}
}
Output:111 Karan
Post a Comment