getter and setter in python توابع التغليف في بايثون
- 0/5
- 2020-12-10
- 2020-12-10
توصيف
توابع التغليف setters and getters مهمتها تغطية اسناد واعطاء قيم للمتحولات في الصنف, من اجل ان تبقى المتحولات خاصة private وان لايمكن الوصول اليها من خارج الصنف الا من خلال التوابع المسموحة لذلك, والتي تسمى getters and setters
لاحظ في المثال التالي يتم اعطاء قيم للكائن Rodger من خلال التابع setColor , وايضا نستطيع اخذ قيم باستدعاء التابع getColor
# Python program to show that we can create
# instance variables inside methods
# Class for Dog
class Dog:
# Class Variable
animal = 'dog'
# The init method or constructor
def __init__(self, breed):
# Instance Variable
self.breed = breed
# Adds an instance variable
def setColor(self, color):
self.color = color
# Retrieves instance variable
def getColor(self):
return self.color
# Driver Code
Rodger = Dog("pug")
Rodger.setColor("brown")
print(Rodger.getColor())
المخرجات:
brown