Java摘要在树中将两个对象视为相同

时间:2014-10-19 06:12:48

标签: java object abstract

我的任务是使用文件夹对象和文件对象对文件系统进行编程,然后使用文件系统对象来管理同一树中的两个对象。我的问题是,我无法弄清楚如何将这两个对象视为一样。

作业说"您可能会发现有一个文件和文件夹继承的抽象类很有帮助,因此您可以对它们进行相同的处理。"但我不断收到错误。

import java.util.*;

public class FileSys {

    private Node firstFolder;

    public void newFolder(String loc) {
        if (firstFolder == null) { // If there are no folders
            Node folder = new Folder(loc); 
            firstFolder = folder;
        }
        else { // If there are folders

            String s = loc; 
            String[] folders = s.split("\\\\"); // Each file/folder name is put into an array

            boolean found; // Flag if found

            Node current = firstFolder; //Sets the first folder to the current
            int n = 0;

            while (folders.length - 1 > n) { // To find the folder being added to

                int i = 0;
                found = false; // Resets flag

                while (current.size > i) { // To search through the names of the folders

                    if (current.next[i].name.equalsIgnoreCase(folders[n])) {
                        current = current.next[i];
                        found = true; // Raises flag
                        break;
                    }

                    i++;
                }

                if ( !found) // incomplete. Add Exception.
                    System.out.println("ERROR");

                n++;
            }

            Node folder = new Folder(folders[folders.length - 1]);
            current.next[current.size] = folder;
        }
    }

    abstract class Node {
        String name;

        Node(String name) {
            this.name = name;
        }
    }



    private class File extends Node {
        String data;

        File(String nm, String data) {
            super(nm);
            this.data = data;
        }
    }


    private class Folder extends Node {
        private static final int ARRAYSIZE = 20; // default array size

        private int size = 0; 
        private Node[] next = new Node[ARRAYSIZE];

        public Folder(String nm) {
            super(nm);
            next[0] = null;
        }
    }

// Main method omitted
}

我很欣赏正确方向的任何帮助!我觉得这是一个非常简单的错误,但我没有足够的经验来处理对象和摘要以了解错误。我尝试过投射,但在运行时会导致更多错误。谢谢!

编辑:

    FileSys.java:55: error: cannot find symbol
                while(current.size > i)
                             ^
  symbol:   variable size
  location: variable current of type FileSys.Node
FileSys.java:57: error: cannot find symbol
                    if(current.next[i].name.equalsIgnoreCase(folders[n]))
                              ^
  symbol:   variable next
  location: variable current of type FileSys.Node
FileSys.java:59: error: cannot find symbol
                        current = current.next[i];
                                         ^
  symbol:   variable next
  location: variable current of type FileSys.Node
FileSys.java:76: error: cannot find symbol
            current.next[current.size] = folder;
                   ^
  symbol:   variable next
  location: variable current of type FileSys.Node
FileSys.java:76: error: cannot find symbol
            current.next[current.size] = folder;
                                ^
  symbol:   variable size
  location: variable current of type FileSys.Node
5 errors

3 个答案:

答案 0 :(得分:1)

将变量声明为类时,只能使用为该类定义的方法调用它。

Node n = new Folder();
n.name; //Fine, all nodes have the name attribute
n.next; //Not fine, nodes do not have a next attribute

解决方案是类型转换。类型转换是告诉编译器的一种方式,"我知道这个对象在运行时将属于这种类型"。

Folder f = (Folder) n; //We are telling the compiler that n is a Folder
f.next; //Fine, because folders have the next attribute

最后一个问题是:我们需要区分文件夹和文件。我们可以使用 instanceof 运算符。

if(n instanceof Folder){
    doFolderStuff();
}else if (n instanceof File){
    doFileStuff()
}

答案 1 :(得分:0)

您正在执行current.sizecurrent.nextsize类中的nextprivate都是Folder。这意味着您无法使用实例访问它们,它们是private类,应该由类内部使用。

因此,为了访问它们,请使用类中使用public访问说明符定义的getter,例如

public int getSize() {
    //return size;
}

希望,这有帮助。

答案 2 :(得分:0)

就组织而言,您可能希望统一您发现类似的对象合同:

public interface FSFilter{
  public boolean accept(FSNode node);
}

public interface FSNode {
  public String getPath();
  public FSNode getParent():
  public Volume getVolume();
  public boolean isFile();
  public boolean isFolder();
  public boolean isVolume();
  public boolean exists();
  public boolean delete();
  public List<FSNode> getSubNodes(boolean recursive, int depth, FSFilter filter);
  public int deleteSubNodes(boolean recursive, int depth, FSFilter filter);
  public long getSize(boolean recursive, int depth, FSFilter filter);
}

具有不同参数组合的上述方法将提供文件系统树可能想要的大多数操作。要将特定函数添加到特定类型的文件系统对象,可以为每个对象提供实现。例如,Volume类可能有额外的diskUsage()方法,File类可能有getInputStream()等。

public class File implements FSNode { //... implementation...//}

public class Folder implements FSNode { //... implementation...//}

public class Volume implements FSNode { //... implementation...//}

然后,您可能希望将适用于所有这些的所有实用程序逻辑放置为无状态静态函数,例如文件的递归列表,删除,创建和修改。因为这些操作通常会处理底层OS调用。这些将等同于您的程序中的lsrmmount等操作系统命令。上面的实现将在内部调用这些实用程序函数。此外,大多数复杂的文件系统代码都驻留在这里:

public final class FSUtil{
  public static List<FSNode> list(boolean recursive, int depth, FSFilter filter){//... logic...//}
  public static long byteSize(FSNode node, boolean recursive, depth, FSFilter filter){//... logic...//}
  public static boolean delete(FSNode node,boolean recursive, FSFilter filter){//... logic...//}
  ... and more ...
}

作为参考,您可以查看现有的实施,例如apache.commons.io