2013-10-31 2 views
-1

나는 아래 상황에서 수퍼 클래스 호출을 어떻게 만들지 알아 내려고하고있다.이 상황에서 수퍼 클래스 전화는 어떻게 만듭니 까?

아래에서 볼 수 있듯이, 확장하고있는 클래스의 서브 클래스는 생성자에 하나의 매개 변수만을 가지고 있으며, 수퍼 클래스에는 2 개가 있습니다. 다음은

내 코드입니다 :

가 어떻게이 상황에서 슈퍼 클래스에 전화를 걸 수 있습니다

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(); 
     } 

    } 
} 
?

+0

TLDR. 아마 SSCCE는 더 많은 hekp를 유치 할 것입니다. – John3136

+2

질문과 관련된 코드 만 포함하면 답을 얻을 확률이 더 높습니다. – Dermot

+0

또는 질문과 관련된 모든 코드가 포함되어있는 경우 대답을 얻을 수있는 기회가 더 많을 수 있습니다. 이것은 많은 의존성을 잃어버린 두 개의 클래스 일뿐입니다. – Ben

답변

0

것은 당신이 DirectorySize의 생성자 내부의 슈퍼 클래스 생성자 DirectoryProcessor(File startingDirectory, FileFilter fileFilter)를 호출 할 경우, 슈퍼 클래스로 하위 클래스 DirectoryProcessorDirectorySize를 참조하고, 제대로 이해하고 경우에, 당신은 첫 번째 문으로 super(startingDirectory, fileFilter)을 추가해야합니다 . super(parameter list) 함께

을하는 매칭 파라미터리스트 수퍼 생성자가 호출된다.

+0

도움을 주셔서 감사합니다. 문제가 해결되었습니다. –

0

당신의 질문은 명확하지 않지만, 파생 클래스 생성자에서 super()을 호출하는 것을 말하는 경우, 그렇습니다. 두 매개 변수를 모두 입력해야합니다.