Python में Object-Oriented Programming
OOP क्या है?
Object-Oriented Programming (OOP) code को objects के इर्द-गिर्द organize करता है — ऐसी चीज़ें जिनमें data (attributes) और actions (methods) होते हैं। class blueprint है; object उससे बनी असली चीज़।
"Student" class blueprint है। Aman और Riya उस blueprint से बने दो objects (students) हैं, हर एक का अपना name और marks।
Class और Object
class Dog:
sound = "Bark" # सभी dogs का साझा attribute
d1 = Dog() # object बनाएं
print(d1.sound)class Dog blueprint है; d1 = Dog() एक असली dog object बनाता है।
__init__ Constructor
__init__ object बनते ही अपने आप चलता है। यह हर object का अपना data set करता है। self current object को दर्शाता है।
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
s1 = Student("Aman", 95)
print(s1.name, s1.marks)Program 1: Student Class
class Student:
def __init__(self, name, roll, marks):
self.name = name
self.roll = roll
self.marks = marks
s1 = Student("Aman", 1, 88)
s2 = Student("Riya", 2, 95)
print(s1.name, "scored", s1.marks)
print(s2.name, "scored", s2.marks)एक class, दो स्वतंत्र objects — हर एक अपना name, roll और marks रखता है।
Program 2: Methods (Behaviour)
Method class के अंदर का function है जो object के data पर काम करता है।
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
def result(self):
return "PASS" if self.marks >= 33 else "FAIL"
s1 = Student("Aman", 75)
print(s1.name, ":", s1.result())Program 3: Inheritance
Inheritance एक class को दूसरी class का code reuse करने देता है। Child को parent से सब मिलता है और वह और जोड़ सकता है।
class Person:
def __init__(self, name):
self.name = name
def greet(self):
print("Hi, I am", self.name)
class Teacher(Person): # Teacher, Person को inherit करता है
def teach(self):
print(self.name, "is teaching")
t = Teacher("Mr. Rao")
t.greet() # Person से
t.teach() # Teacher सेTeacher(Person) मतलब Teacher, Person को inherit करता है — वह greet() मुफ़्त में use कर सकता है।
Program 4: Bank Account (Encapsulation)
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount > self.balance:
print("Insufficient funds")
else:
self.balance -= amount
acc = BankAccount("Aman", 1000)
acc.deposit(500)
acc.withdraw(300)
print("Balance:", acc.balance)Data (balance) और उस पर actions (deposit, withdraw) एक साथ रहते हैं — यही encapsulation है।
OOP के चार स्तंभ
| स्तंभ | मतलब |
|---|---|
| Encapsulation | Data + methods एक class में bundle |
| Inheritance | Child class parent का code reuse करे |
| Polymorphism | एक ही method नाम, अलग behaviour |
| Abstraction | जटिल details छुपाना, सिर्फ ज़रूरी दिखाना |
सामान्य गलतियाँ
- methods के पहले parameter
selfभूलना। - object बनाते समय brackets भूलना:
StudentबनामStudent()। - class (blueprint) और object (असली चीज़) में confusion।
Practice Tasks
- brand और speed वाली
Carclass बनाएं, और details दिखाने वाला method। - Student class में grade print करने वाला method जोड़ें।
Animalparent औरDog/Catchildren बनाएं जोsound()override करें।area()method वाली एक simpleRectangleclass बनाएं।
सारांश
- OOP code को classes (blueprints) और objects (असली items) में organize करता है।
__init__हर object set करता है;selfcurrent object है।- Methods class के अंदर के functions हैं; inheritance code reuse करता है।
- चार स्तंभ: encapsulation, inheritance, polymorphism, abstraction।