现欲构造一文件/目录树,采用组合(Composite)设计模式来设计,得到的类图

最全题库2022-08-02  56

问题 现欲构造一文件/目录树,采用组合(Composite)设计模式来设计,得到的类图如6-1所示:{图}图6-1类图【Java代码】import java.util.ArrayList;import.java.util.List;(1)class AbstractFile{protected String name;public void printName(  ){System.out.println(name);}public abstract boolean addChild(AbstractFile file);public abstract boolean removeChild(AbstracFile file);public abstract List<AbstractFile>getChildren(  );}class File extends AbstractFile{public File(String name){this.name=name;}public?boolean addChild(AbstractFile file){return false;}public?boolean?removeChild(AbstracFile file){return false;}public?List<AbstractFile>getChildren(  ){return(2);}}class Folder extends AbstractFile{private List<AbstracFile>childList;public Folder(String name){this.name=name;this.childList=new ArrayList<AbstractFile>(  );}public boolean addChild(AbstractFile file){return childList.add(file);}public boolean removeChild(AbstractFile file){return childList.remove(file);}public(3)<AbstractFile>getChildren(  ){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.printName(  );List<AbstractFile>children=ifile.getChildren(  ):if(Children==null)return;for(AbstractFile file:Children){(5);}}}该程序运行后输出结果为:c:\compositeTestComposite.javaWindows【Java代码】import java.util.ArrayList;import.java.util.List;(1)class AbstractFile{protected String name;public void printName(  ){System.out.println(name);}public abstract boolean addChild(AbstractFile file);public abstract boolean removeChild(AbstracFile file);public abstract List<AbstractFile>getChildren(  );}class File extends AbstractFile{public File(String name){this.name=name;}public?boolean addChild(AbstractFile file){return false;}public?boolean?removeChild(AbstracFile file){return false;}public?List<AbstractFile>getChildren(  ){return(2);}}class Folder extends AbstractFile{private List<AbstracFile>childList;public Folder(String name){this.name=name;this.childList=new ArrayList<AbstractFile>(  );}public boolean addChild(AbstractFile file){return childList.add(file);}public boolean removeChild(AbstractFile file){return childList.remove(file);}public(3)<AbstractFile>getChildren(  ){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.printName(  );List<AbstractFile>children=ifile.getChildren(  ):if(Children==null)return;for(AbstractFile file:Children){(5);}}}该程序运行后输出结果为:c:\compositeTestComposite.javaWindows

选项

答案

解析 (1)abstract
(2)null
(3)List
(4)childList
(5)printTree(file)
本题考查基本面向对象设计中设计模式的运用能力。
组合设计模式主要是表达整体和部分的关系,并且对整体和部分对象的使用无差别。题目中AbstractFile是File类和Folder类的父类,它抽象了两个类的共有属性和行为,在后续main方法的使用中,不论是File对象还是Folder对象,都可被当作AbstractFile对象来使用。另外,由于Folder对象可以聚合其他的Folder对象和File对象,等价于Folder对象可以聚合另一个AbslractFile对象。
题目中AbstractFile类应该为抽象类,因此其修饰符应该包括abstract,空(2)处返回File类对象的孩子,但File类对象没有孩子节点,因此其返回值应该为NULL。getChildren方法是继承自抽象父类AbstractFile,所以其返回类型应该和父类的定义保持一致,空(4)处返回存储孩子节点的集合对象childList。该程序的运行能够打印出文件目录树,因此空(5)处应该为打印方法的调用。
转载请注明原文地址:https://tihaiku.com/congyezige/2410299.html

最新回复(0)