17. Classes & Objects (OOP)
📘 কনসেপ্ট (থিওরি)
অবজেক্ট-ওরিয়েন্টেড প্রোগ্রামিং (OOP) আপনাকে বাস্তব জিনিসকে কোডে মডেল করতে দেয়। **ক্লাস** হলো একটি নকশা, এবং **অবজেক্ট** হলো সেই ক্লাসের একটি উদাহরণ।
**মূল OOP ধারণা:**
- **Class**: অবজেক্ট তৈরির টেমপ্লেট
- **Object**: ক্লাসের একটি ইনস্ট্যান্স
- **
__init__**: কনস্ট্রাক্টর মেথড, অবজেক্ট তৈরির সময় চলে
- **self**: ক্লাসের বর্তমান ইনস্ট্যান্সকে বোঝায়
- **Attributes**: অবজেক্টের ভেরিয়েবল
- **Methods**: অবজেক্টের ফাংশন
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"I am {self.name}, age {self.age}")
s1 = Student("Rahim", 20)
s1.introduce()
💡 উদাহরণ
ব্যাংক অ্যাকাউন্ট ক্লাস:
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
def deposit(self, amount):
self.balance += amount
print(f"Deposited {amount}. Balance: {self.balance}")
def withdraw(self, amount):
if amount > self.balance:
print("Insufficient funds!")
else:
self.balance -= amount
print(f"Withdrew {amount}. Balance: {self.balance}")
acc = BankAccount("Karim", 1000)
acc.deposit(500)
acc.withdraw(200)
🎯 আপনার কাজ (প্র্যাকটিস)
width এবং height দিয়ে Rectangle ক্লাস তৈরি করুন। area() মেথড যোগ করুন যা width * height রিটার্ন করে। ৫x৩ এর একটি আয়ত তৈরি করে এর ক্ষেত্রফল প্রিন্ট করুন।main.py
Loading...
OUTPUT
Run your code to see the output here...