Python 如何向类的所有对象“广播”消息?
现在我想以某种方式向所有Person对象宣布,我的桶里有一个苹果,如果他们愿意,他们也应该在桶里放一个苹果 只需在类中保留一个普通变量,而不是成员,并在需要向所有类发布消息时进行更新即可Python 如何向类的所有对象“广播”消息?,python,Python,现在我想以某种方式向所有Person对象宣布,我的桶里有一个苹果,如果他们愿意,他们也应该在桶里放一个苹果 只需在类中保留一个普通变量,而不是成员,并在需要向所有类发布消息时进行更新即可 class Person: def __init__(self, name): self.name = name self.bucket = [] def announce(self, *args): # ??? pass
class Person:
def __init__(self, name):
self.name = name
self.bucket = []
def announce(self, *args):
# ???
pass
def put(self, item):
self.bucket.append(item)
p1 = Person("ya")
p2 = Person("yu")
p1.put("apple")
只需在类中保留一个普通变量而不是成员,并在任何时候向所有类宣布某个内容时更新它
class Person:
def __init__(self, name):
self.name = name
self.bucket = []
def announce(self, *args):
# ???
pass
def put(self, item):
self.bucket.append(item)
p1 = Person("ya")
p2 = Person("yu")
p1.put("apple")
简单的实施可以是:
class Person:
bApple = False
def __init__(self, name):
self.name = name
self.bucket = []
def announce(self, *args):
# ???
pass
def put(self, item):
self.bucket.append(item)
def hasApple(self):
if Person.bApple:
return "True"
else:
return "False"
p1 = Person("ya")
p2 = Person("yu")
p1.put("apple")
print "p1 has Apple? " + p1.hasApple()
print "p2 has Apple? " + p2.hasApple()
Person.bApple = True
print "p1 has Apple? " + p1.hasApple()
print "p2 has Apple? " + p2.hasApple()
输出:
class Person:
persons = set()
def __init__(self, name):
self.name = name
self.bucket = []
self.persons.add(self)
def announce(self, msg):
print("[{}]: {}".format(self.name,msg))
@classmethod
def broadcast(cls, person, msg):
for p in cls.persons:
if not p is person:
p.announce(msg)
def put(self, item):
self.bucket.append(item)
self.broadcast(self, '{}: got {} in my bucket!'.format(self.name, item))
p1 = Person("ya")
p2 = Person("yu")
p1.put("apple")
Person.broadcast(None, "Hey! Everybody can broadcast message!")
这一执行缺乏实际意义
不取消注册实施
无线程保存
只能通知Person及其子类
它只是一个玩具,你需要使它适应你的实际情况
也许实现一个比那个简单的更好。简单的实现可能是:
class Person:
bApple = False
def __init__(self, name):
self.name = name
self.bucket = []
def announce(self, *args):
# ???
pass
def put(self, item):
self.bucket.append(item)
def hasApple(self):
if Person.bApple:
return "True"
else:
return "False"
p1 = Person("ya")
p2 = Person("yu")
p1.put("apple")
print "p1 has Apple? " + p1.hasApple()
print "p2 has Apple? " + p2.hasApple()
Person.bApple = True
print "p1 has Apple? " + p1.hasApple()
print "p2 has Apple? " + p2.hasApple()
输出:
class Person:
persons = set()
def __init__(self, name):
self.name = name
self.bucket = []
self.persons.add(self)
def announce(self, msg):
print("[{}]: {}".format(self.name,msg))
@classmethod
def broadcast(cls, person, msg):
for p in cls.persons:
if not p is person:
p.announce(msg)
def put(self, item):
self.bucket.append(item)
self.broadcast(self, '{}: got {} in my bucket!'.format(self.name, item))
p1 = Person("ya")
p2 = Person("yu")
p1.put("apple")
Person.broadcast(None, "Hey! Everybody can broadcast message!")
这一执行缺乏实际意义
不取消注册实施
无线程保存
只能通知Person及其子类
它只是一个玩具,你需要使它适应你的实际情况
也许实现一个比那个简单的更好。用python实现observer模式非常简单, 我希望对象A、对象B、对象C从指定的消息传递对象获取通知的基本思想。因此,您必须以某种方式连接它们,在观察者模式中,此过程称为订阅。因此,您的对象A、B、C观察员正在订阅传递主题消息的对象 这个例子是一个基本的实现。我没有把它添加到你的代码中,但爱丽丝和鲍勃将是你的情况中的人物
[yu]: "ya: got apple in my bucket!
[ya]: Hey! Everybody can broadcast message!
[yu]: Hey! Everybody can broadcast message!
来源:用python实现observer模式非常简单, 我希望对象A、对象B、对象C从指定的消息传递对象获取通知的基本思想。因此,您必须以某种方式连接它们,在观察者模式中,此过程称为订阅。因此,您的对象A、B、C观察员正在订阅传递主题消息的对象 这个例子是一个基本的实现。我没有把它添加到你的代码中,但爱丽丝和鲍勃将是你的情况中的人物
[yu]: "ya: got apple in my bucket!
[ya]: Hey! Everybody can broadcast message!
[yu]: Hey! Everybody can broadcast message!
来源:我不确定你的项目有多大。。但对于这种场景,您应该实现观察者设计模式。我不知道你的项目有多大。。但对于这种场景,您应该实现观察者设计模式。