Class attributes का Inheritance
कोर्स की शुरुआत में, आपने class attributes और methods के बारे में सीखा था जो किसी class की सभी instances के बीच साझा होते हैं. अब देखें कि inheritance के साथ ये कैसे काम करते हैं?
इस अभ्यास में, आप Player class का एक subclass बनाएँगे, जिसके साथ आपने इस चैप्टर में पहले काम किया था, और class attributes व methods के inheritance को समझेंगे.
Player class आपके लिए पहले से परिभाषित है, और यहाँ दिया गया कोड:
class Player:
MAX_POSITION = 10
def __init__(self):
self.position = 0
def move(self, steps):
if self.position + steps < Player.MAX_POSITION:
self.position += steps
else:
self.position = Player.MAX_POSITION
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Object-Oriented Programming परिचय
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Create a Racer class inheriting from Player
____(____):
# Create MAX_POSITION with a value of 15
____ = ____
# Create a Player and a Racer objects
p = ____
r = ____
print("p.MAX_POSITION = ", p.MAX_POSITION)
print("r.MAX_POSITION = ", r.MAX_POSITION)