
admin
2025-10-21 05:47:17
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getImgs() {
return imgs;
}
public void setImgs(String imgs) {
this.imgs = imgs;
}
public ArticleMemento saveToMemento() {
ArticleMemento articleMemento = new ArticleMemento(this.title,this.content,this.imgs);
return articleMemento;
}
public void undoFromMemento(ArticleMemento articleMemento) {
this.title = articleMemento.getTitle();
this.content = articleMemento.getContent();
this.imgs = articleMemento.getImgs();
}
@Override
public String toString() {
return “Editor{” +
“title='” + title + ‘’’ +
“, content='” + content + ‘’’ +
“, imgs='” + imgs + ‘’’ +
‘}’;
}
}
然后创建备忘录角色ArticleMemento类。
public class ArticleMemento {
private String title;
private String content;
private String imgs;
public ArticleMemento(String title, String content, String imgs) {
this.title = title;
this.content = content;
this.imgs = imgs;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
public String getImgs() {
return imgs;
}
@Override
public String toString() {
return “ArticleMemento{” +
“title='” + title + ‘’’ +
“, content='” + content + ‘’’ +
“, imgs='” + imgs + ‘’’ +
‘}’;
}
}
接着创建备忘录管理角色草稿箱DraftsBox类。
public class DraftsBox {
private final Stack STACK = new Stack();
public ArticleMemento getMemento() {
ArticleMemento articleMemento= STACK.pop();
return articleMemento;
}
public void addMemento(ArticleMemento articleMemento) {
STACK.push(articleMemento);
}
}
草稿箱中定义的Stack类是Vector的一个子类,它实现了一个标准的后进先出的栈。如下表所示,主要定义了以下方法。
| 方法定义 | 方法描述 |
| — | — |
| boolean empty() | 测试堆栈是否为空 |
| Object peek( ) | 查看堆栈顶部的对象,但不从堆栈中移除它 |
| Object pop( ) | 移除堆栈顶部的对象,并作为此函数的值返回该对象 |
| Object push(Object element) | 把对象压入堆栈顶部 |
| int search(Object element) | 返回对象在堆栈中的位置,以1为基数 |
最后编写客户端测试代码。
public static void main(String[] args) {
DraftsBox draftsBox = new DraftsBox();
Editor editor = new Editor(“我是这样手写Spring的,麻雀虽小五脏俱全”,
“本文节选自《Spring5核心原理与30个类手写实战》一书,Tom著,电子工业出版社出版。”,
“35576a9ef6fc407aa088eb8280fb1d9d.png”);
ArticleMemento articleMemento = editor.saveToMemento();
draftsBox.addMemento(articleMemento);
System.out.println(“标题:” + editor.getTitle() + “\n” +
“内容:” + editor.getContent() + “\n” +
“插图:” + editor.getImgs() + “\n暂存成功”);
System.out.println(“完整的信息” + editor);
System.out.println(“首次修改文章=”);
editor.setTitle(“【Tom原创】我是这样手写Spring的,麻雀虽小五脏俱全”);
editor.setContent(“本文节选自《Spring5核心原理与30个类手写实战》一书,Tom著”);
System.out.println(“首次修改文章完成=”); 先自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以扫码领取!
总结
以上是字节二面的一些问题,面完之后其实挺后悔的,没有提前把各个知识点都复习到位。现在重新好好复习手上的面试大全资料(含JAVA、MySQL、算法、Redis、JVM、架构、中间件、RabbitMQ、设计模式、Spring等),现在起闭关修炼半个月,争取早日上岸!!!
下面给大家分享下我的面试大全资料
第一份是我的后端JAVA面试大全
后端JAVA面试大全
第二份是MySQL+Redis学习笔记+算法+JVM+JAVA核心知识整理
MySQL+Redis学习笔记算法+JVM+JAVA核心知识整理
第三份是Spring全家桶资料
MySQL+Redis学习笔记算法+JVM+JAVA核心知识整理 家分享下我的面试大全资料**
第一份是我的后端JAVA面试大全
[外链图片转存中…(img-tZR5hFZz-1711517877374)]
后端JAVA面试大全
第二份是MySQL+Redis学习笔记+算法+JVM+JAVA核心知识整理
[外链图片转存中…(img-ltJ47vu0-1711517877374)]
MySQL+Redis学习笔记算法+JVM+JAVA核心知识整理
第三份是Spring全家桶资料
[外链图片转存中…(img-E9r6Ha8v-1711517877374)]
MySQL+Redis学习笔记算法+JVM+JAVA核心知识整理 需要更多Java资料的小伙伴可以帮忙点赞+关注,点击传送门,即可免费领取!