diff --git a/w9/Article.class b/w9/Article.class new file mode 100644 index 0000000..4fc3d0b Binary files /dev/null and b/w9/Article.class differ diff --git a/w9/Article.java b/w9/Article.java new file mode 100644 index 0000000..2f80481 --- /dev/null +++ b/w9/Article.java @@ -0,0 +1,47 @@ +import java.time.LocalDate; + +public class Article { + private String title; + private String content; + private String url; + private String author; + private LocalDate publishDate; + + public Article(String title, String content, String url, String author, LocalDate publishDate) { + this.title = title; + this.content = content; + this.url = url; + this.author = author; + this.publishDate = publishDate; + } + + public String getTitle() { return title; } + public void setTitle(String title) { this.title = title; } + + public String getContent() { return content; } + public void setContent(String content) { this.content = content; } + + public String getUrl() { return url; } + public void setUrl(String url) { this.url = url; } + + public String getAuthor() { return author; } + public void setAuthor(String author) { this.author = author; } + + public LocalDate getPublishDate() { return publishDate; } + public void setPublishDate(LocalDate publishDate) { this.publishDate = publishDate; } + + @Override + public String toString() { + return "Article{" + + "title='" + title + '\'' + + ", author='" + author + '\'' + + ", publishDate=" + publishDate + + ", url='" + url + '\'' + + '}'; + } + + public static void main(String[] args) { + Article article = new Article("Java编程", "内容...", "http://example.com", "张三", LocalDate.of(2024, 1, 15)); + System.out.println(article); + } +} diff --git a/w9/HistoryCommand.class b/w9/HistoryCommand.class new file mode 100644 index 0000000..4421eba Binary files /dev/null and b/w9/HistoryCommand.class differ diff --git a/w9/HistoryCommand.java b/w9/HistoryCommand.java new file mode 100644 index 0000000..fb02c47 --- /dev/null +++ b/w9/HistoryCommand.java @@ -0,0 +1,36 @@ +import java.util.ArrayList; +import java.util.List; + +public class HistoryCommand { + private final List commandHistory = new ArrayList<>(); + + public void recordCommand(String command) { + if (command != null && !command.isBlank()) { + commandHistory.add(command.trim()); + } + } + + public List getHistory() { + return new ArrayList<>(commandHistory); + } + + public void printHistory() { + System.out.println("=== 命令历史 ==="); + for (int i = 0; i < commandHistory.size(); i++) { + System.out.printf("%d: %s%n", i + 1, commandHistory.get(i)); + } + } + + public void clearHistory() { + commandHistory.clear(); + } + + public static void main(String[] args) { + HistoryCommand history = new HistoryCommand(); + history.recordCommand("crawl `https://example.com` "); + history.recordCommand("list"); + history.recordCommand("exit"); + + history.printHistory(); + } +} diff --git a/w9/思考题.txt b/w9/思考题.txt new file mode 100644 index 0000000..c677724 --- /dev/null +++ b/w9/思考题.txt @@ -0,0 +1,3 @@ +分析  List
  共享引用的风险(200字小结) + + List
  的共享引用存在多线程安全与数据一致性风险。当多个模块共享同一个列表引用时,若某模块修改了列表(如增删元素),其他模块读取时会出现并发修改异常或脏数据;若修改了列表内  Article  对象的属性,所有持有该对象引用的模块都会看到变化,引发数据不一致。此外,外部代码可能直接修改列表或对象内部状态,破坏封装性,导致调试困难。解决方式是返回列表副本或不可修改视图,避免共享引用。 \ No newline at end of file