Header Ads

Setter and Getter in Python | Setter and Getter Function in Object Oriented Programming Using Python | Setter and Getter Method in python

In Object Oriented Programming Python Setter and Getter Function is Very Use Full in Classes

Setter Method is Using Set The Value of Data Members
Getter Method is Using Get The value of Data Member

Example : Program Source Code 

#Create Class Book
class Book:
   
#Create Constructor
   
def __init__(self, page = 0, price = 0, name = 'Null'):
       
self.__page = page
       
self.__price = price
       
self.__name = name
   
#Create Setter Method of Book Page Setter
   
def set_Page(self, p):
       
self.__page = p
   
# Create Setter Method of Book Price Setter
   
def set_price(self,pr):
       
self.__price = pr
   
# Create Setter Method of Book Name Setter
   
def set_name(self, n):
       
self.__name = n
   
#Create Getter Method of Book Page Getter
   
def get_page(self):
       
return self.__page
   
# Create Getter Method of Book Price Getter
   
def get_price(self):
       
return self.__price
   
# Create Getter Method of Book Name Getter
   
def get_name(self):
       
return self.__name
#Create object of Book 1
obj1 = Book()
#Setting Book Name using Setter Method
obj1.set_name("C++")
#Setting Book Page using Setter Method
obj1.set_Page(100)
#Setting Book Price using Setter Method
obj1.set_price(2000)
print("-----------Book Information--------------")
#Getting Book Name using Getter Method
print("The Name of Book 1:",obj1.get_name())
#Getting Book Page using Getter Method
print("The Page of Book 1:", obj1.get_page())
#Getting Book Price using Getter Method
print("The Price of Book 1:",obj1.get_price())
#Create object of Book 2
obj2 = Book()
#Setting Book Name using Setter Method
obj2.set_name("Java")
#Setting Book Page using Setter Method
obj2.set_Page(500)
#Setting Book Price using Setter Method
obj2.set_price(2500)
print("-----------Book Information--------------")
#Getting Book Name using Getter Method
print("The Name of Book 2:",obj2.get_name())
#Getting Book Page using Getter Method
print("The Page of Book 2:", obj2.get_page())
#Getting Book Price using Getter Method
print("The Price of Book 2:",obj2.get_price())


Output

Setter, Getter, Getter Method, setter Method, Getter Method in python, setter Method in python,
Setter and Getter Method in python, Setter and Getter in Python, Setter and Getter Function in Object Oriented Programming Using Python

Related Video





 

No comments

Powered by Blogger.