本实例要求编写代码,实现一个基于面向对象思想的、具有添加好友、删除好友、展示好友、退出功能的好友管理系统。
时间: 2023-07-10 13:11:13 浏览: 44
好的,我可以帮您解答这个问题。以下是一个基于面向对象思想的好友管理系统的代码示例:
```python
class Friend:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def __str__(self):
return f"Name: {self.name}, Age: {self.age}, Gender: {self.gender}"
class FriendList:
def __init__(self):
self.friends = []
def add_friend(self, friend):
self.friends.append(friend)
print(f"{friend.name} added to friend list.")
def remove_friend(self, friend):
self.friends.remove(friend)
print(f"{friend.name} removed from friend list.")
def show_friends(self):
if not self.friends:
print("No friends to show.")
else:
print("Friend List:")
for friend in self.friends:
print(friend)
def exit(self):
print("Exiting friend list...")
```
这里定义了两个类,一个是Friend类,表示一个朋友对象,包括姓名、年龄、性别三个属性。另一个是FriendList类,表示好友管理系统,包括添加好友、删除好友、展示好友、退出功能四个方法。
使用这个好友管理系统的例子:
```python
friend_list = FriendList()
friend1 = Friend("Alice", 25, "female")
friend2 = Friend("Bob", 30, "male")
friend_list.add_friend(friend1)
friend_list.add_friend(friend2)
friend_list.show_friends()
friend_list.remove_friend(friend1)
friend_list.show_friends()
friend_list.exit()
```
输出结果:
```
Alice added to friend list.
Bob added to friend list.
Friend List:
Name: Alice, Age: 25, Gender: female
Name: Bob, Age: 30, Gender: male
Alice removed from friend list.
Friend List:
Name: Bob, Age: 30, Gender: male
Exiting friend list...
```
以上就是一个简单的基于面向对象思想的好友管理系统的实现。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)