public class LeafImpl implements Leaf {
private String name;
private Leaf rightLeaf = null;
private Leaf leftLeaf = null;
private Leaf parentLeaf = null;
private boolean root;
private int level = 1;
public Leaf getRightLeaf() {
return rightLeaf;
}
public void setRightLeaf(Leaf rightLeaf) {
this.rightLeaf = rightLeaf;
}
public Leaf getLeftLeaf() {
return leftLeaf;
}
public void setLeftLeaf(Leaf leftLeaf) {
this.leftLeaf = leftLeaf;
}
public LeafImpl(String name) {
this.name = name;
}
public void addParentLeaf(Leaf parentLeaf) {
// TODO 自动生成的构造函数存根
this.parentLeaf = parentLeaf;
}
public void removeParentLeaf() {
// TODO 自动生成的构造函数存根
parentLeaf = null;
}
public Leaf getParentLeaf() {
// TODO 自动生成的构造函数存根
return this.parentLeaf;
}
public Leaf addSubLeaf(Leaf Leaf) {
// TODO 自动生成的构造函数存根
root = true;
if (this.rightLeaf == null) {
//先加右子树
rightLeaf = Leaf;
String name = Leaf.reName(this);
Leaf.setLevel(level + 1);
int i = this.getName().indexOf("_");
int j = this.getName().lastIndexOf("_");
if (i < j) {
String s = this.getName().substring(i, j - 1) + "1";//右子树为1
Leaf.setName(Leaf.getName().concat(s));
} else {
Leaf.setName(name + "_1");
}
Leaf.addParentLeaf(this);
} else if (this.leftLeaf == null) {
//加左子树
leftLeaf = Leaf;
String name = Leaf.reName(this);
int i = this.getName().indexOf("_");
int j = this.getName().lastIndexOf("_");
if (i < j) {
String s = this.getName().substring(i, j - 1) + "2";//左子树为2
Leaf.setName(Leaf.getName().concat(s));
} else {
Leaf.setName(name + "_2");
}
Leaf.setLevel(level + 1);
Leaf.addParentLeaf(this);
}
return this;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLevel() {
return level;
}
public void setLevel(int i) {
level = i;
}
public String reName(Leaf Leaf) {
String name = Leaf.getName();
int index = name.indexOf("_");//返回第一次出现的指定子字符串在此字符串中的索引
char n = (char) ((int) name.charAt(index + 1) + 1);//检验第index+1个字符
String str2 = String.valueOf(name.charAt(index + 1));
String x = String.valueOf(n);
name = name.replaceFirst(str2, x);
this.setName(name);
return name;
}
public void operation() {
this.getLevel();
for (int j = 1; j < level; j++) {
System.out.print(" ");
}
System.out.println(this.getName());
if (root) {
for (int i = 1; i < this.getRightLeaf().getLevel(); i++) {
System.out.print(" ");
}
this.getRightLeaf().operation();
for (int i = 1; i < this.getLeftLeaf().getLevel(); i++) {
System.out.print(" ");
}
this.getLeftLeaf().operation();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
代码.zip (90个子文件)
设计模式代码
Decorator
.project 385B
src
Shangpin.java 249B
Milk.java 157B
Xiaopiao.java 201B
Client.java 791B
Bread.java 154B
Coke.java 155B
.settings
org.eclipse.jdt.core.prefs 603B
.classpath 307B
bin
Coke.class 528B
Client.class 1KB
Bread.class 531B
Milk.class 528B
Xiaopiao.class 592B
Shangpin.class 483B
Composite
.project 385B
src
ConcreteBuilder.java 579B
Director.java 255B
LeafImpl.java 3KB
Builder.java 92B
Leaf.java 474B
Client.java 438B
.settings
org.eclipse.jdt.core.prefs 598B
.classpath 299B
bin
Leaf.class 494B
Builder.class 155B
ConcreteBuilder.class 1KB
Client.class 1KB
Director.class 571B
LeafImpl.class 3KB
Singleton
.project 385B
src
lazy
lazySingleton.java 541B
Client.java 351B
eager
eagerSingleton.java 460B
Client.java 371B
.settings
org.eclipse.jdt.core.prefs 603B
.classpath 307B
bin
lazy
Client.class 716B
lazySingleton.class 486B
eager
eagerSingleton.class 476B
Client.class 724B
Factory
.project 383B
src
SubSystem2.java 103B
SubSystem3.java 105B
Adapter.java 114B
Stra1.java 146B
WagerFactory2.java 120B
Stra2.java 148B
Adaptee.java 107B
Target.java 51B
Proxy.java 217B
Gamble.java 50B
Facade.java 412B
Wager2.java 392B
Cash.java 152B
Stra3.java 148B
SubSystem1.java 103B
WagerFactory1.java 120B
GamblingFactory.java 80B
Strategy.java 61B
Wager1.java 376B
Wager.java 70B
Client.java 1001B
ConcreteTarget.java 130B
Context.java 210B
.settings
org.eclipse.jdt.core.prefs 598B
.classpath 299B
bin
Facade.class 922B
WagerFactory2.class 366B
ConcreteTarget.class 534B
SubSystem1.class 488B
SubSystem3.class 491B
Wager2.class 757B
Stra3.class 497B
Wager1.class 784B
Client.class 2KB
Adaptee.class 505B
Wager.class 258B
SubSystem2.class 488B
Stra2.class 497B
Adapter.class 359B
Stra1.class 497B
Gamble.class 117B
Context.class 453B
Strategy.class 125B
WagerFactory1.class 366B
Cash.class 474B
Target.class 122B
GamblingFactory.class 300B
Proxy.class 399B
共 90 条
- 1
资源评论
meibing6627
- 粉丝: 3
- 资源: 16
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功