import java.util.ArrayList;import java.util.List;abstract class AbstractFile{
protected String name; public void printName(){System.out.println(name);} public abstract boolean addChild(AbstractFile file); public abstract boolean removeChild(AbstractFile 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
(AbstractFile file){
return false;
}
public List<AbstractFile> getChildren(){return null;}
}
class Folder extends AbstractFile
{
private List <AbstractFile> 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 List<AbstractFile> getChildren(){
return childList ;
}
}
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) { printTree(file) ;
}
}
} 为什么输出是import java.util.ArrayList;import java.util.List;abstract class AbstractFile{
protected String name; public void printName(){System.out.println(name);} public abstract boolean addChild(AbstractFile file); public abstract boolean removeChild(AbstractFile 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
(AbstractFile file){
return false;
}
public List<AbstractFile> getChildren(){return null;}
}
class Folder extends AbstractFile
{
private List <AbstractFile> 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 List<AbstractFile> getChildren(){
return childList ;
}
}
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) { printTree(file) ;
}
}
}
c:\
composite
TestComposite.java
windows
Press any key to continue...
protected String name; public void printName(){System.out.println(name);} public abstract boolean addChild(AbstractFile file); public abstract boolean removeChild(AbstractFile 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
(AbstractFile file){
return false;
}
public List<AbstractFile> getChildren(){return null;}
}
class Folder extends AbstractFile
{
private List <AbstractFile> 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 List<AbstractFile> getChildren(){
return childList ;
}
}
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) { printTree(file) ;
}
}
} 为什么输出是import java.util.ArrayList;import java.util.List;abstract class AbstractFile{
protected String name; public void printName(){System.out.println(name);} public abstract boolean addChild(AbstractFile file); public abstract boolean removeChild(AbstractFile 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
(AbstractFile file){
return false;
}
public List<AbstractFile> getChildren(){return null;}
}
class Folder extends AbstractFile
{
private List <AbstractFile> 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 List<AbstractFile> getChildren(){
return childList ;
}
}
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) { printTree(file) ;
}
}
}
c:\
composite
TestComposite.java
windows
Press any key to continue...
输出每个abstractfile的时候(即printTree方法),在输出当前目录之后,还会递归输出当前目录的儿子,也就是
1 printTree开始,当前目录C:\
1 打印 C:\
1 循环子节点
1.1 递归调用printTree开始,当前目录composite
1.1 打印 composite
1.1 循环子节点
1.1.1 递归调用printTree开始,文件TestComposite.java
1.1.1 打印TestComposite.java
1.1.1 当前没有子节点,跳出当前递归,回到1.1
1.1 子节点循环完毕,跳出当前递归,回到1
1 继续循环子节点
1.2 递归调用printTree开始,当前目录windows
1.2 打印 windows
1.2 当前没有子节点,跳出当前递归,回到1
1 当前子节点循环结束,最外层的printTree运行完毕