From 65507b2e083d936602c032b16c30c3f8d0de71fc Mon Sep 17 00:00:00 2001 From: Fuyuxinge <1876397977@qq.com> Date: Thu, 14 May 2026 17:13:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=B9=B6=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w8/kv.java | 31 +++++++++++++++++++++++++++++++ w8/kv1.java | 32 ++++++++++++++++++++++++++++++++ w9/Article.java | 22 ++++++++++++++++++++++ w9/HistoryCommand.java | 15 +++++++++++++++ w9/zongjie.txt | 1 + 5 files changed, 101 insertions(+) create mode 100644 w8/kv.java create mode 100644 w8/kv1.java create mode 100644 w9/Article.java create mode 100644 w9/HistoryCommand.java create mode 100644 w9/zongjie.txt diff --git a/w8/kv.java b/w8/kv.java new file mode 100644 index 0000000..42ffdf1 --- /dev/null +++ b/w8/kv.java @@ -0,0 +1,31 @@ +package w8; + +public class Pair { + private K key; + private V value; + + public Pair(K key, V value) { + this.key = key; + this.value = value; + } + + public Pair swap() { + return new Pair<>(this.value, this.key); + } + + public K getKey() { + return key; + } + + public void setKey(K key) { + this.key = key; + } + + public V getValue() { + return value; + } + + public void setValue(V value) { + this.value = value; + } +} diff --git a/w8/kv1.java b/w8/kv1.java new file mode 100644 index 0000000..3693f2e --- /dev/null +++ b/w8/kv1.java @@ -0,0 +1,32 @@ +package w8; + +import java.util.HashMap; +import java.util.Map; + +public class Cache { + private final Map cacheMap; + + public Cache() { + cacheMap = new HashMap<>(); + } + + public void put(K key, V value) { + cacheMap.put(key, value); + } + + public V get(K key) { + return cacheMap.get(key); + } + + public void remove(K key) { + cacheMap.remove(key); + } + + public void clear() { + cacheMap.clear(); + } + + public int size() { + return cacheMap.size(); + } +} \ No newline at end of file diff --git a/w9/Article.java b/w9/Article.java new file mode 100644 index 0000000..649e1f4 --- /dev/null +++ b/w9/Article.java @@ -0,0 +1,22 @@ +package w9; + +import java.util.Date; +public class Article { + private String title; + private String content; + private String author; + private Date publishDate; + + public Article() {} + public Article(String title, String content, String author, Date publishDate) { + this.title = title; + this.content = content; + this.author = author; + this.publishDate = publishDate; + } + + public String getAuthor() { return author; } + public void setAuthor(String author) { this.author = author; } + public Date getPublishDate() { return publishDate; } + public void setPublishDate(Date publishDate) { this.publishDate = publishDate; } +} \ No newline at end of file diff --git a/w9/HistoryCommand.java b/w9/HistoryCommand.java new file mode 100644 index 0000000..220baed --- /dev/null +++ b/w9/HistoryCommand.java @@ -0,0 +1,15 @@ +package w9; + +import java.util.ArrayList; +import java.util.List; +public class HistoryCommand { + private List commandList = new ArrayList<>(); + + + public void addCommand(String cmd) { + commandList.add(cmd); + } + public List getHistory() { + return new ArrayList<>(commandList); + } +} \ No newline at end of file diff --git a/w9/zongjie.txt b/w9/zongjie.txt new file mode 100644 index 0000000..3b27e1a --- /dev/null +++ b/w9/zongjie.txt @@ -0,0 +1 @@ +直接返回的List
是引用传递,外部代码可直接修改集合内 Article 对象或增删元素,存在数据安全风险。其一,外部修改 Article 的标题、作者等字段,会直接篡改内存中原始数据,引发数据不一致;其二,外部可随意新增、删除集合元素,破坏数据完整性;其三,多线程场景下,共享集合无同步机制,易出现并发异常、脏数据。需返回集合副本或不可变集合,避免外部直接操作原始数据。 \ No newline at end of file