slots_3.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. class Device:
  2. __slots__ = '_name', '_location', '_status'
  3. def __init__(self, name, location, status='ON'):
  4. self._name = name
  5. self.location = location
  6. self.status = status
  7. @property
  8. def name(self):
  9. return self._name
  10. @property
  11. def location(self):
  12. return self._location
  13. @location.setter
  14. def location(self, value):
  15. self._location = value
  16. @property
  17. def status(self):
  18. return self._status
  19. @status.setter
  20. def status(self, value):
  21. self._status = value
  22. def turn_on(self):
  23. self.status = 'ON'
  24. def turn_off(self):
  25. self.status = 'OFF'
  26. class Light(Device):
  27. __slots__ = '_brightness', '_color'
  28. def __init__(self, name, location, brightness, color):
  29. super().__init__(name, location)
  30. self.brightness = brightness
  31. self._color = color
  32. @property
  33. def brightness(self):
  34. return self._brightness
  35. @brightness.setter
  36. def brightness(self, value):
  37. self._brightness = value
  38. @property
  39. def color(self):
  40. return self._color
  41. class Thermostat(Device):
  42. __slots__ = '_current_temperature', '_target_temperature'
  43. def __init__(self, name, location, current_temperature, target_temperature):
  44. super().__init__(name, location)
  45. self.current_temperature = current_temperature
  46. self.target_temperature = target_temperature
  47. @property
  48. def current_temperature(self):
  49. return self._current_temperature
  50. @current_temperature.setter
  51. def current_temperature(self, value):
  52. self._current_temperature = value
  53. @property
  54. def target_temperature(self):
  55. return self._target_temperature
  56. @target_temperature.setter
  57. def target_temperature(self, value):
  58. self._target_temperature = value
  59. class SmartTV(Device):
  60. __slots__ = '_channel'
  61. def __init__(self, name, location, channel):
  62. super().__init__(name, location)
  63. self.channel = channel
  64. @property
  65. def channel(self):
  66. return self._channel
  67. @channel.setter
  68. def channel(self, value):
  69. self._channel = value
  70. def main():
  71. '''
  72. dev = Device('robot-bobot', 'Moscow', 'on')
  73. print(dev.name)
  74. light = Light('light-bobot', 'Moscow', 'on', 70, 'red')
  75. print(light.name)
  76. print(light.brightness)
  77. print(light.color)
  78. '''
  79. device1 = Device('Устройство 1', 'Гостиная')
  80. assert device1.name == 'Устройство 1'
  81. assert device1._name == 'Устройство 1'
  82. assert device1.location == 'Гостиная'
  83. assert device1._location == 'Гостиная'
  84. assert device1.status == 'ON'
  85. assert device1._status == 'ON'
  86. device1.turn_off()
  87. assert device1.status == 'OFF'
  88. device1.location = 'Кухня'
  89. assert device1.location == 'Кухня'
  90. assert device1._location == 'Кухня'
  91. device1.turn_on()
  92. assert device1.status == 'ON'
  93. light1 = Light('Лампа', 'Гостиная', 50, 'белый')
  94. light1.name == 'Лампа'
  95. light1.location == 'Гостиная'
  96. light1.status == 'ON'
  97. light1.brightness == '50'
  98. light1.color == 'белый'
  99. light1.turn_off()
  100. light1.status == 'OFF'
  101. thermostat_1 = Thermostat('Термометр', 'Балкон', 10, 15)
  102. thermostat_1.name == 'Термометр'
  103. thermostat_1.location == 'Балкон'
  104. thermostat_1.status == 'ON'
  105. thermostat_1.current_temperature == 10
  106. thermostat_1.target_temperature == 15
  107. tv = SmartTV('Samsung', 'Спальня', 20)
  108. tv.name == 'Термометр'
  109. tv.location == 'Балкон'
  110. tv.status == 'ON'
  111. tv.channel == 20
  112. print('GOOD')
  113. if __name__ == '__main__':
  114. main()