2013-10-24 94 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方法,它将检查实例以及类。

相关问题