以下是一个用于保存获取帖子结构的代码示例:
class Post:
def __init__(self, title, content, author):
self.title = title
self.content = content
self.author = author
def get_title(self):
return self.title
def get_content(self):
return self.content
def get_author(self):
return self.author
# 创建一个帖子列表
posts = []
# 将帖子添加到列表中
post1 = Post("标题1", "内容1", "作者1")
posts.append(post1)
post2 = Post("标题2", "内容2", "作者2")
posts.append(post2)
# 获取帖子列表中的帖子
for post in posts:
print("标题:", post.get_title())
print("内容:", post.get_content())
print("作者:", post.get_author())
print()
上述代码定义了一个Post
类,用于保存帖子的结构。每个帖子对象都有一个标题、内容和作者。通过get_title()
、get_content()
和get_author()
方法,可以获取帖子的标题、内容和作者信息。
在示例中,首先创建了一个空的帖子列表posts
。然后,创建了两个帖子对象,并将它们添加到列表中。最后,通过遍历帖子列表,获取每个帖子的标题、内容和作者信息,并进行打印输出。
你可以根据自己的需求,进一步扩展Post
类的功能,例如添加时间戳、评论等属性和方法。