Constructor
Constructors ក្នុង Java
🏗️ Constructor គឺជាអ្វី?
Constructor គឺ special method ដែលរត់ស្វ័យប្រវត្តិនៅពេលបង្កើត object មួយ។ វាប្រើដើម្បី initialize (set តម្លៃចាប់ផ្តើម) សម្រាប់ object។
ឧទាហរណ៍ក្នុងជីវិតពិត:
- បង្កើតគណនី: Constructor = កំណត់ឈ្មោះ, សមតុល្យចាប់ផ្តើម
- ចូលសាលារៀន: Constructor = set ឈ្មោះ, អាយុ, ថ្នាក់សិស្ស
- បើក App: Constructor = setup មុនពេលប្រើ
📚 ហេតុអ្វីត្រូវការ Constructor?
- Initialize ស្វ័យប្រវត្តិ: set តម្លៃចាប់ផ្តើមដោយ automatic
- កូដខ្លី: មិនចាំបាច់ set មួយៗ ក្រោយពេលបង្កើត
- កំណត់លក្ខខណ្ឌ: ត្រូវមានតម្លៃចាំបាច់
- Validate data: ពិនិត្យទិន្នន័យមុនបង្កើត
🔑 ចំណុចសម្គាល់ Constructor:
- ឈ្មោះស្មើ class: Constructor ត្រូវឈ្មោះដូចគ្នានឹង class
- គ្មាន return type: មិនដូច void, int, String នៅមុខ
- ហៅស្វ័យប្រវត្តិ: នៅពេលប្រើ
new
keyword
📊 Constructor vs Method
លក្ខណៈ | Constructor | Method |
---|---|---|
ឈ្មោះ | ស្មើ class | អ្វីក៏បាន |
Return type | គ្មាន | មាន (void, int, etc.) |
នៅពេលហៅ | ពេលបង្កើត object | នៅពេលចាំបាច់ |
មុខងារ | Initialize object | សកម្មភាពណាមួយ |
ឧទាហរណ៍ | Person() { } | void walk() { } |
📦 ប្រភេទ Constructors
- Default Constructor: គ្មាន parameters
- Parameterized Constructor: មាន parameters
- Copy Constructor: copy ពី object មួយ
Java Code
Click "Run" to execute the Java code
🔁 Constructor Overloading
🤹 Overloading គឺជាអ្វី?
Constructor Overloading គឺការមាន constructors ច្រើន ក្នុង class តែមួយ ដោយ parameters ខុសគ្នា។
🎯 ហេតុអ្វីប្រើ Overloading?
- Flexibility: បង្កើត object ដោយវិធីខុសគ្នា
- Optional data: អាចផ្តល់ទិន្នន័យ ឬ មិនផ្តល់
- Convenience: បង្កើតគ្រប់ស្ថានភាព
📝 ឧទាហរណ៍ Constructor Overloading
class Person {
String name;
int age;
String city;
// Constructor 1: គ្មាន parameters
Person() {
this.name = "Unknown";
this.age = 0;
this.city = "Unknown";
}
// Constructor 2: ឈ្មោះ និង អាយុ
Person(String name, int age) {
this.name = name;
this.age = age;
this.city = "Unknown";
}
// Constructor 3: គ្រប់ទិន្នន័យ
Person(String name, int age, String city) {
this.name = name;
this.age = age;
this.city = city;
}
}
// ប្រើប្រាស់
Person p1 = new Person(); // Constructor 1
Person p2 = new Person("Dara", 25); // Constructor 2
Person p3 = new Person("Srey", 23, "PP"); // Constructor 3
🔗 Constructor Chaining
ហៅ constructor មួយពី constructor មួយដៃទ ប្រើ this()
class Student {
String name;
int age;
String grade;
// Constructor 1
Student() {
this("Unknown", 0); // ហៅ Constructor 2
}
// Constructor 2
Student(String name, int age) {
this(name, age, "A"); // ហៅ Constructor 3
}
// Constructor 3 - Main constructor
Student(String name, int age, String grade) {
this.name = name;
this.age = age;
this.grade = grade;
}
}
// ប្រើ
Student s1 = new Student(); // → Unknown, 0, A
Student s2 = new Student("Dara", 20); // → Dara, 20, A
Student s3 = new Student("Srey", 19, "B"); // → Srey, 19, B
🧠 this Keyword
this
មាន 3 ការប្រើ:
- this.variable: សំដៅទៅ instance variable
- this.method(): ហៅ instance method
- this(): ហៅ constructor ដៃទ
class Box {
int width;
int height;
Box(int width, int height) {
this.width = width; // this.width = instance variable
this.height = height; // width = parameter
}
void display() {
System.out.println(this.width + " x " + this.height);
}
}
🎯 Real-World Example
class Product {
String name;
double price;
int quantity;
String category;
// សម្រាប់ផលិតផលទូទៅ
Product(String name, double price) {
this(name, price, 0, "General");
}
// សម្រាប់ផលិតផលមាន stock
Product(String name, double price, int quantity) {
this(name, price, quantity, "General");
}
// Constructor ពេញលេញ (មានគ្រប់ទិន្នន័យ)
Product(String name, double price, int quantity, String category) {
this.name = name;
this.price = price;
this.quantity = quantity;
this.category = category;
System.out.println("បង្កើតផលិតផល: " + this.name);
}
void displayInfo() {
System.out.println("ឈ្មោះ: " + this.name);
System.out.println("តម្លៃ: $" + this.price);
System.out.println("Stock: " + this.quantity);
System.out.println("ប្រភេទ: " + this.category);
}
}
// ប្រើប្រាស់
Product p1 = new Product("Phone", 500.0);
Product p2 = new Product("Laptop", 1200.0, 10);
Product p3 = new Product("Mouse", 25.0, 50, "Electronics");
⚠️ Common Mistakes
កំហុស | មូលហេតុ | ដំណោះស្រាយ |
---|---|---|
Return type ក្នុង constructor | មិនត្រូវមាន return type | ដកចេញ |
ឈ្មោះមិនត្រឹមត្រូវ | Constructor ត្រូវឈ្មោះស្មើ class | ប្តូរឈ្មោះ |
this() នៅទីមិនត្រឹមត្រូវ | this() ត្រូវនៅ 1st line | ដាក់នៅបន្ទាត់ទី 1 |
💡 Best Practices
- Use this: ប្រើ this ដើម្បីបែងចែក parameter និង instance variable
- Constructor chaining: ដាក់ logic មួយកន្លែង
- Validation: ពិនិត្យ input ក្នុង constructor
- Default values: ផ្តល់តម្លៃ default សម្រាប់ data ដែលមិនមាន
Java Code
Click "Run" to execute the Java code