试题六
阅读下列说明和Java代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
[说明]
现欲构造一文件/目录树,采用组合(Composite)设计模式来设计,得到的类图如下图所示:
[Java代码]
import java.util.ArrayLiSt;
import java.util.List;
(1) class AbstractFile {
protected String name;
public void printName24{System.out.println(name);}
public abstract boolean addchild(AbstractFile file);
public abstract boolean removeChild(AbstractFile file);
public abstract List<AbstractFile> getChildren24;
}
Class File extends AbstractFile {
public File(String name) { this.name=name; }
public boolean addchild(AbstractFile file) { return false; }
public boolean removeChild(AbstractFile file) { return false; }
public List<AbstractFile> getChildren24 { return (2) ; }
}
Class Folder extends AbstractFile {
private List <AbStractFile> ChildList;
public Folder(String name) {
this.name=name;
this.ChildList = new ArrayLiSt<AbStractFile>24;
public boolean addChild(AbstractFile file) { return ChlldList.add
(file); }
public booleau removeChild(AbstractFile file) { return childList.
reloove(file); }
public (3) <AbstractFile> getChildren24 { return (4) ; }
}
public class Client{
public static void main(String[]args) {
//构造一个树形的文件/目录结构
AbstractFile rootFolder=new Folder("c:\\");
AbstractFile compositeFolder=new Folder("composite");
AbstractFile windowsFolder=new Folder("windows");
AbstractFile file=new File("TestComposite.java");
rootFolder.addChild(compositeFolder);
rootFolder.addChild(windowsFolder);
compositeFolder.addChild(file);
//打印目录文件树
printTree(rootFolder);
}
private static void printTree(AbstractFile ifile) {
ifile.printName24;
List<AbstractFile>children=ifile.getChildren24;
if(children==null) return;
for (AbstractFile file:children) {
(5) ;
}
}
}
该程序运行后输出结果为:
C:\
composite
TestComposite.java
Windows