Advertisement
Lyuben_Andreev

Свойства

Aug 13th, 2024
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | Software | 0 0
  1. import random
  2. from datetime import date
  3.  
  4.  
  5. class Employee:
  6.     hobby = 'Cooking'
  7.  
  8.     def __init__(self, firstName, lastName, age):
  9.         self.firstName = firstName
  10.         self.lastName = lastName
  11.         self._age = age
  12.         self._employee_id = random.randint(1, 100)
  13.  
  14.     def _show_id(self):
  15.         print(self._employee_id)
  16.  
  17.     def get_info(self):
  18.         return (f'Employee first name - {self.firstName} '
  19.                 f'last name - {self.lastName} '
  20.                 f'age - {self._age}')
  21.  
  22.     def say_hi(self, greeting):
  23.         print(f'{greeting}, I am {self.firstName}.')
  24.  
  25.     @staticmethod
  26.     def say_greeting():
  27.         print('Nice to meet you!')
  28.  
  29.     @classmethod
  30.     def set_default_hobby(cls, hobby):
  31.         cls.hobby = hobby
  32.  
  33.     @classmethod
  34.     def get_age_from_year(cls, firstName, lastName, yearBirth):
  35.         employeeAge = date.today().year - yearBirth
  36.         return cls(firstName, lastName, employeeAge)
  37.  
  38.     @classmethod
  39.     def create_emp_from_str(cls, emp_data: str):
  40.         firstName, lastName, age = emp_data.split()
  41.         return cls(firstName, lastName, int(age))
  42.  
  43.     # Property for age with getter and setter
  44.     @property
  45.     def age(self):
  46.         return self._age
  47.  
  48.     @age.setter
  49.     def age(self, value):
  50.         if value < 0:
  51.             raise ValueError("Age cannot be negative.")
  52.         self._age = value
  53.  
  54.     # Property for employee_id (read-only)
  55.     @property
  56.     def employee_id(self):
  57.         return self._employee_id
  58.  
  59. # Creating instances and using the class methods
  60. employee1 = Employee('Peter', 'Б', 35)
  61. print(employee1.get_info())
  62. print(f'Hobby: {employee1.hobby}')
  63. employee1.say_hi('Hi')
  64. employee1.say_greeting()
  65.  
  66. # Setting and getting hobby using a class method
  67. Employee.set_default_hobby('Football')
  68. print(f'Updated Hobby: {employee1.hobby}')
  69.  
  70. # Creating an employee from birth year
  71. employee3 = Employee.get_age_from_year('Jane', 'Doe', 1995)
  72. print(employee3.get_info())
  73.  
  74. # Creating an employee from a string
  75. employee4 = Employee.create_emp_from_str('Andy Jophnes 19')
  76. print(employee4.get_info())
  77. print(f'First Name: {employee4.firstName}')
  78.  
  79. # Using property to get and set age
  80. print(f"Employee's age: {employee1.age}")
  81. employee1.age = 36
  82. print(f"Updated age: {employee1.age}")
  83.  
  84. # Accessing read-only employee_id
  85. print(f"Employee ID: {employee1.employee_id}")
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement