通过封装Comment类并使用ArrayList管理评论,实现博客评论模块的增删查功能,提升面向对象编程与集合框架应用能力。

在Java中实现一个简单的博客评论模块,重点在于合理地进行对象封装,并结合集合类来管理多个评论数据。这个过程不仅能锻炼面向对象编程能力,也能加深对集合框架的理解和应用。
通过private修饰属性,提供公共的getter和setter方法,保证数据的安全性和可操作性。同时可以重写toString()方法方便输出查看。
示例代码:
public class Comment {
private int id;
private String author;
private String content;
private String createTime;
public Comment() {}
public Comment(int id, String author, String content, String createTime) {
this.id = id;
this.author = author;
this.content = content;
this.createTime = createTime;
}
// getter 和 setter 方法
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getAuthor() { return author; }
public void setAuthor(String author) { this.author = author; }
public String getContent() { return content; }
public void setContent(String content) { this.content = content; }
public String getCreateTime() { return createTime; }
public void setCreateTime(String createTime) { this.createTime = createTime; }
@Override
public String toString() {
return "Comment{" +
"id=" + id +
", author='" + author + '\'' +
", content='" + content + '\'' +
", createTime='" + createTime + '\'' +
'}';
}
}
可以在主程序或服务类中定义一个List
import java.util.ArrayList;
import java.util.List;
public class BlogCommentManager {
private List<Comment> comments;
public BlogCommentManager() {
comments = new ArrayList<>();
}
// 添加评论
public void addComment(Comment comment) {
comments.add(comment);
System.out.println("评论已添加:" + comment.getAuthor());
}
// 删除评论(按ID)
public boolean deleteCommentById(int id) {
for (Comment c : comments) {
if (c.getId() == id) {
comments.remove(c);
System.out.println("已删除ID为 " + id + " 的评论");
return true;
}
}
System.out.println("未找到ID为 " + id + " 的评论");
return false;
}
// 显示所有评论
public void displayAllComments() {
if (comments.isEmpty()) {
System.out.println("暂无评论");
return;
}
System.out.println("=== 所有评论 ===");
for (Comment c : comments) {
System.out.println(c);
}
}
// 根据作者查找评论
public void findCommentsByAuthor(String author) {
System.out.println("查找作者为 '" + author + "' 的评论:");
boolean found = false;
for (Comment c : comments) {
if (c.getAuthor().equals(author)) {
System.out.println(c);
found = true;
}
}
if (!found) {
System.out.println("未找到该作者的评论");
}
}
}
public class Main {
public static void main(String[] args) {
BlogCommentManager manager = new BlogCommentManager();
// 创建几条评论
Comment c1 = new Comment(1, "张三", "这篇文章写得很好!", "2025-04-01 10:00");
Comment c2 = new Comment(2, "李四", "学习到了新知识,谢谢分享", "2025-04-01 10:30");
Comment c3 = new Comment(3, "张三", "下次希望能更详细一点", "2025-04-01 11:00");
// 添加评论
manager.addComment(c1);
manager.addComment(c2);
manager.addComment(c3);
// 显示全部
manager.displayAllComments();
// 按作者查询
manager.findCommentsByAuthor("张三");
// 删除一条评论
manager.deleteCommentById(2);
// 再次查看
manager.displayAllComments();
}
}
以上就是Java制作简单博客评论模块_对象封装与集合综合使用的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号