python编写简单的朋友圈系统
时间: 2023-11-15 09:01:11 浏览: 25
好的,下面是一个简单的Python朋友圈系统的实现:
```python
class Friend:
def __init__(self, name):
self.name = name
self.posts = []
def add_post(self, post):
self.posts.append(post)
def view_posts(self):
for post in self.posts:
print(post)
class Post:
def __init__(self, content):
self.content = content
def __str__(self):
return self.content
class FriendCircle:
def __init__(self):
self.friends = {}
def add_friend(self, friend):
self.friends[friend.name] = friend
def add_post(self, friend_name, post):
self.friends[friend_name].add_post(post)
def view_posts(self, friend_name):
self.friends[friend_name].view_posts()
```
使用方法:
```python
# 创建朋友圈
fc = FriendCircle()
# 添加好友
alice = Friend("Alice")
bob = Friend("Bob")
fc.add_friend(alice)
fc.add_friend(bob)
# 发布动态
alice.add_post(Post("Hello, world!"))
bob.add_post(Post("I love Python!"))
alice.add_post(Post("Today is a beautiful day."))
bob.add_post(Post("I'm learning machine learning."))
# 查看动态
fc.view_posts("Alice")
fc.view_posts("Bob")
```
输出结果:
```
Hello, world!
Today is a beautiful day.
I love Python!
I'm learning machine learning.
```