Class Method In OOP

# Class Method - Which takes class as argument #

class Employee:
no_of_leaves = 8 # class variable

# Constructer = Use to initialize objects
def __init__(self,name,salary,role,workinghrs):
self.name = name
self.salary = salary
self.role = role
self.workinghrs = workinghrs

# Method
def printdetails(self):
return f"The Name is {self.name},Salary is {self.salary},Role is {self.role} and Working Hours are {self.workinghrs}"

@classmethod # Decorator #
def change_leaves(cls,newleaves):
cls.no_of_leaves = newleaves

emp1 = Employee("EMP1",99999,"Senior Manager",11) # First Object
emp2 = Employee("EMP2",88888,"Manager",12) # Second Object

emp1.change_leaves(19) # Class variable is changed by the object with the help of Class Method #
Employee.change_leaves(29) # Changed by the class #
emp2.change_leaves(9) # Again changed by the object #
print(emp1.no_of_leaves)

Comments

Popular posts from this blog

Python Program To Store Marks

Currency.txt

Python Comprehensions