【问题标题】:How do I make a super class call in this situation?在这种情况下,我该如何拨打超级班级电话?
【发布时间】:2013-10-31 02:10:49
【问题描述】:

我正试图弄清楚如何在以下情况下进行超类调用。

正如您将在下面看到的,我正在扩展的类的子类在其构造函数中只有一个参数,而超类有两个。

下面是我的代码:

    import java.io.*;
    import java.util.*;

    public class DirectorySize extends DirectoryProcessor
    {
        /*
        Dan Czarnecki
        October 29, 2013

        Class variables:
            private HashMap<File, DirectoryStatistics> directory
                A HashMap object that will contain the files and folders
                within a directory

        Constructors:
            public DirectorySize(File startingDirectory) throws FileNotFoundException
                Creates a DirectorySize object, takes in a pathname (inherited from DirectoryProcessor class,
                and has a single hashmap of a DirectoryStatistics object to hold the files and folders
                within a directory

        Methods:
            public void processFile(File file)
                A method that searches for all the files and folders
                within a directory and calculated the total size
                for each of them
            public void run()
                Calls the processFile() method and prints out
                the files and folders (along with their sizes)
                in the directory
            public String toString()
                Returns each element in the directory Vector


        Modification History
            October 29, 2013
                Original version of class
                Implemented run() and processFile() methods
            October 30, 2013
                Added implementation of FileFilter to processFile() method
        */
        private HashMap<File, DirectoryStatistics> directory;

        public DirectorySize(File startingDirectory) throws FileNotFoundException
        {
            super(startingDirectory);
            directory = new HashMap <File, DirectoryStatistics>();
        }


        public void processFile(File file, FileFilter fileFilter) throws FileNotFoundException
        {
            DirectoryStatistics parent;
            int index;
            File parentFile;
            boolean find;


            parentFile = file.getParentFile();
            parent = new DirectoryStatistics(parentFile);
            find = directory.containsValue(parent);

            if(find)
            {
                directory.put(parentFile, parent);
                directory.get(parentFile).setSizeInBytes(file.length());
                directory.get(parentFile).incrementFileCount();
            }


            if(find)
            {
                directory.get(find).addToSizeInBytes(file.length()); //increments the total size of the directory
                directory.get(find).incrementFileCount(); //increments the number of files in the directory
            }


        }

        public void run() throws FileNotFoundException
        {
            File file;
            file = directoryLister.next();


            while(file != null) //the following will be called when the File object is not null
            {
                processFile(file, fileFilter);
                file = directoryLister.next();
            }

        }

        public String toString()
        {
            Set<File> parentFile = directory.keySet();
            File[] parentFileArray = parentFile.toArray(new File[0]);

            String str; //creates a new string that will hold the file or directory name
            str = "";

            for(int i = 0; i < parentFileArray.length; i++)
            {
                str = str + directory.get(parentFileArray[i]).toString();
            }

            return str;
        }
    }
import java.io.*;

abstract public class DirectoryProcessor extends DirectoryLister
{
    /*
    Dan Czarnecki
    October 14, 2013

    Class variables:
        private DirectoryLister directoryLister
            A variable of type DirectoryLister that will list all the directories from a given pathname

    Constructors:
        public DirectoryProcessor(File startingDirectory) throws FileNotFoundException
            Makes a super call to the startingDirectory variable, creates a new DirectoryLister object

    Methods:
        abstract public void processFile(File file, FileFilter fileFilter)
            This abstract method's function is implemented in the TotalFileSize class to process each file within a directory and return the total size of them

        public void run()
            Basically takes the functionality what was originally done with our FileLister and DirectoryLister class (within the driver class)
            but has now moved to its own method

    Modification History:
        October 14, 2013
            Original version of class

        October 16, 2013
            Class and processFile() method have been made abstract

        October 30, 2013

    */

    public DirectoryLister directoryLister;
    public FileFilter fileFilter;

    public DirectoryProcessor(File startingDirectory, FileFilter fileFilter) throws FileNotFoundException
    {
        super(startingDirectory);
        directoryLister = new DirectoryLister(startingDirectory);
        fileFilter = this.fileFilter;
    }

    abstract public void processFile(File file, FileFilter fileFilter) throws FileNotFoundException;


    public void run() throws FileNotFoundException
    {
        File file;
        file = directoryLister.next();

        while(file != null) //the following will be called when the File object is not null
        {
            processFile(file, fileFilter);
            file = directoryLister.next();
        }

    }
}

在这种情况下如何调用超类?

【问题讨论】:

  • TLDR。也许 SSCCE 会吸引更多的关注。
  • 如果您包含仅与您的问题相关的代码,您可能更有可能获得答案
  • 或者,如果您包含与您的问题相关的所有代码。这只是缺少很多依赖的两个类。

标签: java superclass super


【解决方案1】:

如果我对您的理解正确,您将DirectorySize 称为子类DirectoryProcessor 称为超类,如果您想在DirectorySize 的构造函数中调用超类构造函数DirectoryProcessor(File startingDirectory, FileFilter fileFilter),那么您将拥有将super(startingDirectory, fileFilter) 添加为第一条语句。

使用super(parameter list),超类构造函数带有 调用匹配的参数列表。

【讨论】:

  • 感谢您的帮助,这个问题现在已经为我解决了。
【解决方案2】:

您的问题不清楚,但如果您指的是在派生类构造函数中调用super(),是的,您必须提供所需的两个参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多