Search This Blog

162 Python Class

 12) Python Class

#!/usr/bin/python
# creating class
class Employee:
   'base class for all employees'
   
   def __init__(self, f_name, l_name, age):
      self.f_name = f_name
      self.l_name = l_name
      self.age = age
         
   def displayEmployee(self):
      print ("First Name : ", self.f_name, "Last Name :", self.l_name, "Age: ", self.age)

#creating instance of objects
"This would create first object of Employee class"
emp1 = Employee("John", "Cena", 45)
"This would create second object of Employee class"
emp2 = Employee("Peter", "Parker", 37)

# calling/accessing attributes
emp1.displayEmployee()
emp2.displayEmployee()
O/p:
First Name :  John Last Name : Cena Age:  45
First Name :  Peter Last Name : Parker Age:  37

Class Inheritance:

No comments:

Post a Comment