Python OOP in Hindi | पाइथन की ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग समझें
📘 Chapter 10: Object Oriented Programming (OOP)
Object Oriented Programming (OOP) एक प्रोग्रामिंग स्टाइल है जिसमें प्रोग्राम को "objects" और "classes" के रूप में लिखा जाता है। यह कोड को ज्यादा व्यवस्थित और reusable बनाता है।
🔹 Class और Object क्या होता है?
Class: एक blueprint या ढांचा है जिससे object बनाए जाते हैं।
Object: Class का एक उदाहरण (instance) होता है।
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
def display(self):
print("नाम:", self.name)
print("रोल:", self.roll)
# Object बनाना
s1 = Student("राहुल", 101)
s1.display()
🔹 Constructor क्या होता है?
__init__()
एक स्पेशल method है जो object बनाते समय अपने आप चलता है।
🔹 Encapsulation (एनकैप्सुलेशन):
डेटा को छुपाना और उसे method के द्वारा एक्सेस करना – इसे encapsulation कहते हैं।
class Employee:
def __init__(self):
self.__salary = 50000 # private attribute
def get_salary(self):
return self.__salary
🔹 Inheritance (विरासत):
एक class दूसरी class की properties को use कर सकती है – इसे inheritance कहते हैं।
class Animal:
def speak(self):
print("Animal बोल रहा है")
class Dog(Animal):
def bark(self):
print("Dog भौंक रहा है")
d = Dog()
d.speak()
d.bark()
🔹 Polymorphism (बहुरूपीता):
एक ही method का अलग-अलग behavior होना – इसे polymorphism कहते हैं।
class Bird:
def sound(self):
print("चूं चूं")
class Duck(Bird):
def sound(self):
print("क्वैक क्वैक")
obj = Duck()
obj.sound() # क्वैक क्वैक
📌 इस चैप्टर में आपने सीखा:
- Class और Object क्या होता है
- Constructor का प्रयोग
- Encapsulation, Inheritance और Polymorphism
- OOP से कोड क्यों ज्यादा मजबूत और organized बनता है
➡️ अगला चैप्टर:
Chapter 11: पाइथन में डेटा बेस कनेक्शन (Python Database Connectivity – SQLite)
Post a Comment
0 Comments