package com.shejimoshi.jiegouxingmoshi.learjava;
public class TextEditor {
private StringBuilder builder = new StringBuilder();
public void add(char ch){
builder.append(ch);}
public void add(String s){
builder.append(s);}
public void delete(){
if(builder.length()>0){
builder.deleteCharAt(builder.length() - 1);}}
public String getState(){return builder.toString();}
public void setState(String state){
this.builder.delete(0,this.builder.length());
this.builder.append(state);}
public void print(){
System.out.println(this.builder.toString());}}
package com.shejimoshi.jiegouxingmoshi.learjava;
public class Main {
public staticvoidmain(String[] args){
TextEditor editor = new TextEditor();
editor.add("Hello");
editor.add(",");
editor.delete();
editor.add(' ');
editor.add("world");//保存状态
String state = editor.getState();//继续编辑
editor.add("!!!");
editor.delete();
editor.print();//恢复状态
editor.setState(state);
editor.print();}}