简体   繁体   English

尝试从另一个类的方法中调用方法

[英]Trying to call a method in a method from another class

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

I'm trying to call the method toonBoot from the class Boot. 我正在尝试从Boot类调用方法toonBoot。 This should be done for every tweedeboot of the type Boot (same as the class) from the ArrayList boten. 对于ArrayList boten中Boot类型(与类相同)的每个tweedeboot,都应这样做。 toonBoot prints a few lines of information (basically it's a number of coordinates). toonBoot打印几行信息(基本上是许多坐标)。

For some reason, I always receive the error "non-static method toonBoot() cannot be referenced from a static context". 由于某些原因,我总是收到错误“无法从静态上下文引用非静态方法toonBoot()”。 What am I doing wrong? 我究竟做错了什么? Thanks! 谢谢!

You have to call method on instance . 您必须在instance上调用method。

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

Where 哪里

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

See: 看到:

What you are doing 你在做什么

By calling the method from the Class name , you're telling the compiler that this method is a static method. 通过从Class name调用该方法,就可以告诉编译器该方法是static方法。 That is, calling Boot.hello() that the method signature for hello() is something like: 也就是说,调用Boot.hello()hello()的方法签名类似于:

public static void hello() {}

What you should do 你应该怎么做

Call from the object reference, or in this case tweedeboot . 从对象引用(在这种情况下为tweedeboot This tells the compiler that the method is either a static method or an instance method, and it will check the instance as well as the class. 这告诉编译器该方法是static方法还是instance方法,它将检查实例以及类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM