系统消息 用户1 查看了 标记为已读 用户2为查看则标记为未读 python怎么实现
假设我们有一个消息类 Message,它有两个属性:id 和 is_read。is_read 表示是否已读,0 表示未读,1 表示已读。
我们可以定义一个方法 check_read,接受一个用户 id 作为参数,如果这个用户已经查看了这个消息,则将 is_read 设为 1,否则设为 0。
代码如下:
class Message:
def __init__(self, id):
self.id = id
self.is_read = 0
def check_read(self, user_id):
if user_id == 1:
self.is_read = 1
else:
self.is_read = 0
使用方法:
msg = Message(1)
msg.check_read(1) # 用户1查看了,设为已读
msg.is_read # 输出 1
msg.check_read(2) # 用户2未查看,设为未读
msg.is_read # 输出 0
``
原文地址: https://www.cveoy.top/t/topic/fuZA 著作权归作者所有。请勿转载和采集!