Advertisement
eshafik

python constructor

Feb 9th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. class Employee:
  2.    'Common base class for all employees'
  3.    empCount = 0
  4.    
  5.     #object constructor with arguments
  6.    def __init__(self, name, salary):
  7.       self.name = name
  8.       self.salary = salary
  9.       Employee.empCount += 1 #predefined function/work during creation of object instance
  10.    
  11.    def displayCount(self):
  12.      print("Total Employee {}".format(Employee.empCount))
  13.  
  14.    def displayEmployee(self):
  15.       print("Name: {0}, Salary: {1}".format(self.name,self.salary))
  16.  
  17. "This would create first object of Employee class"
  18. emp1 = Employee("Zara", 2000)
  19. "This would create second object of Employee class"
  20. emp2 = Employee("Manni", 5000)
  21. emp1.displayEmployee()
  22. emp2.displayEmployee()
  23. print("Total Empolyee {}".format(Employee.empCount))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement