1.您已经被招募加入一个正在为智能城市开发软件的软件团队。右边的图表显示了一个配备了红外和超声波传感器的智能垃圾桶的部分解决方案;红外传感器检测运动(当垃圾桶盖打开和关闭时),超声波传感器测量垃圾桶盖和垃圾桶内容物之间的空间量——用来测量它的满量。您已经得到了与另一个程序员制作的这个设计对应的代码
class Sensor:
__doc__ = 'Simple Sensor Class'
def __init__(self, ra, acc, sid, **kwargs):
super().__init__(**kwargs)
self._range = ra
self._accuracy = acc
self._sensor_id = sid
def get_range(self):
return self._range
def get_id(self):
return self._sensor_id
class InfraRedSensor(Sensor):
__doc__ = 'Infra Red Sensor Class'
def __init__(self, mot, ty, **kwargs):
super().__init__(**kwargs)
self._max_op_temp = mot
self._type = ty
def get_type(self):
return self._type
def detect_motion(self):
# Simulating a motion detection event generated by the sensor
return True
class UltraSonicSensor(Sensor):
__doc__ = 'Ultra Sonic Sensor Class'
def __init__(self, freq, **kwargs):
super().__init__(**kwargs)
self._operating_freq = freq
def get_distance(self):
# Simulating a distance measured by the sensor (in cm)
return 75
class SmartBin(InfraRedSensor, UltraSonicSensor):
__doc__ = 'SmartBin Class'
"""Represent smart bin – using multiple inh. - bad solution"""
def __init__(self, cap, **kwargs):
super().__init__(**kwargs)
self._capacity = cap
self._fill_level = 0
def get_capacity(self):
return self._capacity
def get_fill_level(self):
return self._fill_level
def update_fill_level(self):
""" Computes the amount of available space in the bin."""
# We assume here that an empty bin will have 100cm of free space
# between the bin lid and the bottom when empty.
self._fill_level = (100 - self.get_distance()) / 100
# mysens1 = SmartBin(cap=500,freq=400.2,mot=30,ty='Passive',ra=300,acc=1.0,sid='abc1234')
# print(mysens1.get_capacity())
# print(mysens1._fill_level)
# mysens1.update_fill_level()
# print(mysens1._fill_level)
研究这段代码并重新编写它,以创建一个避免使用多重继承的新版本(sensors_no_mi.py)。使用聚合(具有)关系。
solution:
# PROBLEM 2
# Avoiding use of multiple inheritance.
class Sensor:
__doc__ = 'Simple Sensor Class'
def __init__(self, ra, acc, sid):
self._range = ra
self._accuracy = acc
self._sensor_id = sid
def get_range(self):
return self._range
def get_id(self):
return self._sensor_id
class InfraRedSensor(Sensor):
__doc__ = 'Infra Red Sensor Class'
def __init__(self, ra, acc, sid, mot=30, ty='Passive'):
super().__init__(ra, acc, sid)
self._max_op_temp = mot
self._type = ty
def get_type(self):
return self._type
def detect_motion(self):
# Simulating a motion detection event generated by the sensor
return True
class UltraSonicSensor(Sensor):
__doc__ = 'Ultra Sonic Sensor Class'
def __init__(self, ra, acc, sid, freq=24.0):
super().__init__(ra, acc, sid)
self._operating_freq = freq
def get_distance(self):
# Simulating a distance measured by the sensor (in cm)
return 12.5
class SmartBin:
__doc__ = 'SmartBin Class'
"""Represent smart bin – without using multiple inh."""
# Pass an IRSensor and USSensor object to the SmartBin initializer
# This way, the sensors exist without the bin (aggregation)
def __init__(self, ir, us, cap=500):
self._capacity = cap
self._fill_level = 0
self._ir_sensor = ir
self._us_sensor = us
def get_capacity(self):
return self._capacity
def get_fill_level(self):
return self._fill_level
def update_fill_level(self):
""" Computes the amount of available space in the bin."""
# We assume here that an empty bin will have 100cm of free space
# between the bin lid and the bottom when empty.
self._fill_level = (100 - self._us_sensor.get_distance()) / 100 * self._capacity
# us_sensor = UltraSonicSensor(100, 1.0, 's1234',18.6)
# ir_sensor = InfraRedSensor(500, 0.5, 's8967')
# mybin = SmartBin(ir_sensor, us_sensor, 600)
# print(mybin.get_capacity())
# print(mybin._fill_level)
# mybin.update_fill_level()
# print(mybin._fill_level)