Python | Crear clase Empleado

Empleado class en Python: Este es un ejemplo de class Python y objeto, aquí; vamos a implementar un class Empleado en Python ?

Python – código class empleado

# employee class code in Python
# class definition
class Employee:
__id=0
__name=""
__gender=""
__city=""
__salary=0
# function to set data
def setData(self,id,name,gender,city,salary):
self.__id=id
self.__name = name
self.__gender = gender
self.__city = city
self.__salary = salary
# function to get/print data
def showData(self):
print("Idtt:",self.__id)
print("Namet:", self.__name)
print("Gendert:", self.__gender)
print("Cityt:", self.__city)
print("Salaryt:", self.__salary)
# main function definition
def main():
#Employee Object
emp=Employee()
emp.setData(1,'pankaj','male','delhi',55000)
emp.showData()
if __name__=="__main__":
main()

salida

Id      : 1
Name : pankaj
Gender : male
City : delhi
Salary : 55000

Otro ejemplo: Obtención de valores de la función de

class Employee:
__id=0
__name=""
__gender=""
__city=""
__salary=0
def setData(self):
self.__id=int(input("Enter Id:t"))
self.__name = input("Enter Name:t")
self.__gender = input("Enter Gender:t")
self.__city = input("Enter City:t")
self.__salary = int(input("Enter Salary:t"))
def showData(self):
print("Idtt:",self.__id)
print("Namet:", self.__name)
print("Gendert:", self.__gender)
print("Cityt:", self.__city)
print("Salaryt:", self.__salary)
def main():
#Employee Object
emp=Employee()
emp.setData()
emp.showData()
if __name__=="__main__":
main()

salida

Enter Id:       1
Enter Name: pankaj
Enter Gender: male
Enter City: delhi
Enter Salary: 55000
Id : 1
Name : pankaj
Gender : male
City : delhi
Salary : 55000


Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *