5 changed files with 86 additions and 0 deletions
Binary file not shown.
@ -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); |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
@ -0,0 +1,36 @@ |
|||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
public class HistoryCommand { |
||||
|
private final List<String> commandHistory = new ArrayList<>(); |
||||
|
|
||||
|
public void recordCommand(String command) { |
||||
|
if (command != null && !command.isBlank()) { |
||||
|
commandHistory.add(command.trim()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public List<String> 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(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,3 @@ |
|||||
|
分析 List<Article> 共享引用的风险(200字小结) |
||||
|
|
||||
|
List<Article> 的共享引用存在多线程安全与数据一致性风险。当多个模块共享同一个列表引用时,若某模块修改了列表(如增删元素),其他模块读取时会出现并发修改异常或脏数据;若修改了列表内 Article 对象的属性,所有持有该对象引用的模块都会看到变化,引发数据不一致。此外,外部代码可能直接修改列表或对象内部状态,破坏封装性,导致调试困难。解决方式是返回列表副本或不可修改视图,避免共享引用。 |
||||
Loading…
Reference in new issue