2010-11-21 90 views
0

我一直在試圖創建一個方法(在一個單獨的類中),它將String作爲參數並使用預製的SoundEngine(不同的類)對象來播放該文件作爲字符串輸入。這是迄今爲止的代碼。問題出現在
SoundEngine.playCompletely(file);Java非靜態方法playCompletely不能從靜態上下文中引用

import java.util.ArrayList; 

public class AudioFileList 
{ 
    // Field for a quantity of notes. 
    private ArrayList<String> files; 

    // Constructor that initializes the array field. 
    public AudioFileList() 
    { 
     files = new ArrayList<String>(); 
    } 

    // Method to add file names to the collection. 
    public void addFile(String file) 
    { 
     files.add(file); 
    } 

    // Method to return number of files in the collection. 
    public int getNumberOfFiles() 
    { 
     return files.size(); 
    } 

    // Method to print the strings in the collection. 
    public void listFiles() 
    { 
     int index = 0; 
     while(index < files.size()) 
     { 
      System.out.println(index + ":" + files.get(index)); 
      index++; 
      } 
    } 

    // Method that removes an item from the collection. 
    public void removeFile(int fileNumber) 
    { 
     if(fileNumber < 0) 
      { 
       System.out.println ("This is not a valid file number"); 
      } 
     else if(fileNumber < getNumberOfFiles()) 
      { 
       files.remove(fileNumber); 
      } 
     else 
      { 
       System.out.println ("This is not a valid file number"); 
      } 
    }  

    // Method that causes the files in the collection to be played. 

    public void playFile(String file) 
    { 
    SoundEngine.playCompletely(file); 
    } 

} 

任何幫助非常感謝。

+1

當你輸入你的問題,右邊是標記一箱**如何格式化**。值得一讀。我已經採取了初步措施爲您設置代碼格式,因爲我無法立即回想您是否可以通過rep = 1編輯您的問題... – 2010-11-21 14:17:35

+0

問題的格式不能幫助我們正確地解答您的問題,請先修復它。爲此使用'101010'。 //在調用它的非靜態方法之前,你應該創建一個'SoundEngine'的實例,或者使該方法靜態。 – khachik 2010-11-21 14:18:43

+0

@khachik對不起,您是什麼意思由'101010'? – thelost 2010-11-21 14:21:00

回答

3

SoundEngineplayCompletely函數是一個實例函數,而不是一個類函數。因此,而不是:

SoundEngine.playCompletely(file); // Compilation error 

你想:

// First, create an instance of SoundEngine 
SoundEngine se = new SoundEngine(); 

// Then use that instance's `playCompletely` function 
se.playCompletely(file); 
相關問題