OOP - Classes & Objects
Object-Oriented Programming (OOP)
🏛️ OOP គឺជាអ្វី?
OOP (Object-Oriented Programming) គឺរបៀបសរសេរកម្មវិធីដែលប្រើ Objects និង Classes។
ឧទាហរណ៍ក្នុងជីវិតពិត:
គ្រប់អ្វីមួយក្នុងជីវិតគឺ object:
- រថយន្ត: មាន properties (ពណ៌, ម៉ាក, ប្រភេទ) និង methods (បើក, រត់, ស្តុប)
- ទូរស័ព្ទ: មាន properties (លេខ, គណនី, ឈ្មោះ) និង methods (ហៅ, send, លប)
- និស្សិត: មាន properties (ឈ្មោះ, អាយុ, class) និង methods (សិក្សា, ប្រលង)
📚 4 ដំណាក់កាលមូលដ្ឋាន OOP
- Encapsulation - ការលាក់ទិន្នន័យ (Data hiding)
- Inheritance - ការទទួលមរដក (Code reuse)
- Polymorphism - ប្រភេទច្រើន (Many forms)
- Abstraction - ការលាក់ស្មុគស្មាញ (Hide complexity)
📦 Class vs Object
Class | Object |
---|---|
ផែនការ (Blueprint) | វត្ថុពិតប្រាកដ (Instance) |
Template | Real thing |
កំណត់រចនា | បង្កើតពី class |
តែមួយ | ច្រើន |
Car class | myCar, yourCar objects |
🏗️ ឧទាហរណ៍ Blueprint
// Class = Blueprint របស់ផ្ទះ
class House {
int rooms; // Properties
String color;
void openDoor() { // Methods
System.out.println("បើកទ្វារ");
}
}
// Objects = ផ្ទះពិតប្រាកដបង្កើតពី blueprint
House myHouse = new House(); // ផ្ទះទី 1
House yourHouse = new House(); // ផ្ទះទី 2
myHouse.rooms = 5;
yourHouse.rooms = 3;
🎯 ប្រយោជន៍ OOP
- Reusability: ប្រើ class ម្តងទៀត បង្កើត objects ច្រើន
- Modularity: កូដទៅជាផ្នែកតូចៗ
- Maintainability: កែ class មួយ មិនប៉ោះពាល់ទៅចំណេកដៃទ។
- Security: Encapsulation បាក់ប្រើទិន្នន័យ
- Real-world modeling: ដូចវត្ថុក្នុងជីវិតពិត
Java Code
Click "Run" to execute the Java code
📦 Instance Variables & Methods
� Instance Variables (Attributes/Properties)
Instance variables គឺ variables ក្នុង class ដែលមានតម្លៃខុសគ្នាសម្រាប់ object នីមួយៗ។
class Student {
// Instance variables
String name; // សម្រាប់ object
int age;
double score;
boolean isActive;
}
🔧 Instance Methods
Instance methods គឺ methods ដែលប្រើ object ហៅ។
class Student {
String name;
int age;
// Instance method
void display() {
System.out.println(ឈ្មោះ: " + name);
System.out.println("អាយុ: " + age);
}
void study() {
System.out.println(name + " is studying");
}
}
📊 Instance vs Static
លក្ខណៈ | Instance | Static |
---|---|---|
ទៅកាន់ | Object | Class |
ការចូលប្រើ | ត្រូវការ object | មិនចាំបាច់ object |
Memory | ទុករាល់ object មួយ copy | Shared រវាង objects |
ឧទាហរណ៍ | name, age, display() | count, PI, main() |
📦 Creating & Using Objects
// 1. បង្កើត object
Student student1 = new Student();
// 2. Set values
student1.name = "Dara";
student1.age = 20;
// 3. ហៅ method
student1.display();
// បង្កើត objects ច្រើន
Student student2 = new Student();
student2.name = "Srey";
student2.age = 19;
🧠 this Keyword
this
សំដៅទៅកាន់ object បច្ចុប្បន្ន។
class Person {
String name;
void setName(String name) {
this.name = name; // this.name = instance variable
// name = parameter
}
void display() {
System.out.println(this.name); // current object's name
}
}
🎯 Real-World Example
class BankAccount {
String accountNumber;
String ownerName;
double balance;
void deposit(double amount) {
this.balance += amount;
System.out.println("បានបន្ថែម: $" + amount);
System.out.println("សមតុល្យបច្ចុប្បន្ន: $" + this.balance);
}
void withdraw(double amount) {
if (this.balance >= amount) {
this.balance -= amount;
System.out.println("ទាញចេញ: $" + amount);
} else {
System.out.println("សមតុល្យមិនគ្រប់!");
}
}
void checkBalance() {
System.out.println("សមតុល្យ: $" + this.balance);
}
}
// ប្រើប្រាស់
BankAccount myAccount = new BankAccount();
myAccount.ownerName = "Dara";
myAccount.balance = 1000.0;
myAccount.deposit(500); // +$500
myAccount.withdraw(200); // -$200
myAccount.checkBalance(); // $1300
💡 Best Practices
- Class names: PascalCase (Student, BankAccount)
- Variable/method names: camelCase (firstName, getAge)
- Encapsulation: ប្រើ private variables + public methods
- Single Responsibility: Class មួយត្រូវមាន 1 មុខងារច្បាស់
Java Code
Click "Run" to execute the Java code