2013-10-24 93 views
1
public void toonBoten() 
    { 
     for(Boot tweedeboot: boten) 
     { 
      Boot.toonBoot(); 
     } 
    } 

我想從類Boot調用方法toonBoot。這應該對ArrayList boten中的Boot類型的每個tweedeboot(與類相同)完成。 toonBoot打印幾行信息(基本上它是一些座標)。試圖從另一個類的方法調用方法

出於某種原因,我總是收到錯誤「無法從靜態上下文中引用非靜態方法toonBoot()」。 我在做什麼錯? 謝謝!

+0

你怎麼稱呼當前的方法? –

回答

4

您必須撥打電話instance

public void toonBoten() 
    { 
     for(Boot tweedeboot: boten) 
     { 
      tweedeboot.toonBoot(); 
     } 
    } 

Boot.toonBoot(); //means toonBoot() is a static method in Boot class 

參見:

1

你在做什麼

通過調用Class name中的方法,您告訴編譯器該方法是static方法。也就是說,調用Boot.hello()對於hello()方法簽名是這樣的:

public static void hello() {} 

你應該做的,從對象引用

呼叫,或在這種情況下tweedeboot。這告訴編譯器該方法是static方法或instance方法,它將檢查實例以及類。

相關問題