本文介绍了Java中静态方法调用非静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在静态类中调用非静态方法时出现错误.

I'm getting an error when I try to call a non-static method in a static class.

不能从类型回放中静态引用非静态方法methodName()

我不能将方法设为静态,因为这也会给我一个错误.

I can't make the method static as this gives me an error too.

此静态方法无法对 xInterface 隐藏实例方法

有没有办法绕过在另一个静态方法中调用非静态方法?(这两个方法在单独的包和单独的类中).

Is there any way to get round calling an non-static method in another static method? (The two methods are in seperate packages and seperate classes).

推荐答案

从静态方法调用非静态方法的唯一方法是拥有一个包含非静态方法的类的实例.根据定义,非静态方法是在某个类的实例上调用的方法,而静态方法属于该类本身.

The only way to call a non-static method from a static method is to have an instance of the class containing the non-static method. By definition, a non-static method is one that is called ON an instance of some class, whereas a static method belongs to the class itself.

这篇关于Java中静态方法调用非静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 21:23
查看更多