}
}
文件夹:
import java.util.LinkedList;
import java.util.List;
/**
- 文件夹。
*/
public class Dir extends Component {
private List components = new LinkedList<>();
private String name;
public Dir(String name) {
this.name = name;
}
@Override
public void addChild(Component child) {
getChild().add(child);
}
@Override
public List getChild() {
return components;
}
@Override
public void print(String s) {
System.out.println(s + “==” + this.name);
if (getChild() != null) {
s += “\t”;
for (Component c : getChild()) {
c.print(s);
}
}
}
}
测试:
private void test() {
Component dir_a = new Dir(“文件夹A”);
Component file_a1 = new File(“文件A1”);
Component file_a2 = new File(“文件A2”);
dir_a.addChild(file_a1);
dir_a.addChild(file_a2);
Component dir_b = new Dir(“文件夹 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 B”);
Component file_b1 = new File(“文件B1”);
Component file_b2 = new File(“文件B2”);
dir_b.addChild(file_b1);
dir_b.addChild(file_b2);