是否可以创建一个具有某些属性=其他类的对象的类?
类传感器:
def __init__(self):
# The attributes of the class that will be available for external use
self.temperature = None
self.humidity = None
self.distance = None
self.light = None
self.sound = None
self.url = None
self.base_url = "http://..."
# Instantiating sensor objects
self.SOUND = NEW OBJECT FROM CLASS SOUND
self.LIGHT = NEW OBJECT FROM CLASS LIGHT
self.DISTANCE = NEW OBJECT FROM CLASS DISTANCE
self.TEMP = NEW OBJECT FROM CLASS TEMPERATURE
答案 0 :(得分:1)
你试过吗?
>>> class A(object):
... pass
...
>>> class B(object):
... a = A()
...
>>> b = B()
>>> b.a
<__main__.A object at 0x021490B0>