繁体   English   中英

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

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

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

我正在尝试从Boot类调用方法toonBoot。 对于ArrayList boten中Boot类型(与类相同)的每个tweedeboot,都应这样做。 toonBoot打印几行信息(基本上是许多坐标)。

由于某些原因,我总是收到错误“无法从静态上下文引用非静态方法toonBoot()”。 我究竟做错了什么? 谢谢!

您必须在instance上调用method。

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

哪里

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

看到:

你在做什么

通过从Class name调用该方法,就可以告诉编译器该方法是static方法。 也就是说,调用Boot.hello()hello()的方法签名类似于:

public static void hello() {}

你应该怎么做

从对象引用(在这种情况下为tweedeboot 这告诉编译器该方法是static方法还是instance方法,它将检查实例以及类。

暂无
暂无

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

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