java中从main方法调用另一个方法
我有
class foo{
public static void main(String[] args){
do();
}
public void do(){}
}
,但是当我通过在命令行上运行命令 java foo
从 main
调用 do()
时,java 抱怨你不能' t 从静态函数调用方法。
所以我的问题是:如何从主方法调用方法,如果不可能,在使用 java 命令从命令行运行程序后调用方法有哪些替代策略。
I have
class foo{
public static void main(String[] args){
do();
}
public void do(){}
}
but then when I call do()
from main
by running the command java foo
on the command line, java complains that you can't call a method from a static function.
So my question is: How do you call methods from the main method and if it is not possible what are some alternative strategies to call methods after the program is run from the command line using the java command.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您只能针对类的实例调用像
do()
这样的实例方法(顺便说一句,这是一个非法的方法名称):或者,也可以将
doSomething()
设为静态,如果这适合您的设计。You can only call instance method like
do()
(which is an illegal method name, incidentally) against an instance of the class:Alternatively, make
doSomething()
static as well, if that works for your design.检查 main 方法之前的 static ,这将该方法声明为类方法,这意味着它不需要调用实例。因此,当您要调用非静态方法时,Java 会抱怨,因为您正在尝试调用所谓的“实例方法”,这当然首先需要一个实例;)
如果您想更好地了解类和实例,请创建一个带有实例和类方法的新类,在主循环中创建一个对象并调用方法!
另请记住,Java 中的类应以大写字母开头。
Check out for the static before the main method, this declares the method as a class method, which means it needs no instance to be called. So as you are going to call a non static method, Java complains because you are trying to call a so called "instance method", which, of course needs an instance first ;)
If you want a better understanding about classes and instances, create a new class with instance and class methods, create a object in your main loop and call the methods!
Also remember, classes in Java should start with an uppercase letter.
这是 Java 的基本理解,但对于新程序员来说可能有点棘手。对静态方法和实例方法之间的区别进行一些研究。基本区别是实例方法 do() 只能由类 foo 的实例访问。
您必须实例化该类(创建其实例),创建一个用于调用实例方法的对象。
我已将您的示例包含在一些评论和示例中。
This is a fundamental understanding in Java, but can be a little tricky to new programmers. Do a little research on the difference between a static and instance method. The basic difference is the instance method do() is only accessible to a instance of the class foo.
You must instantiate (create an instance of) the class, creating an object, that you use to call the instance method.
I have included your example with a couple comments and example.
您可以通过多种方式做到这一点。这里有两个。干杯!
You can do it multiple ways. Here are two. Cheers!
如果你想在你的 main 方法中使用
do()
,有 2 个选择,因为一个是静态的,但另一个 (do()) 不new Foo( ).do();
static do()
方法看看这个 Sun 教程
If you want to use
do()
in your main method there are 2 choices because one is static but other (do()) notnew Foo().do();
static do()
methodHave a look at this sun tutorial
首先java不允许你有
do()
方法。相反,您可以将其设为doOperation()
。其次,您不能从静态函数直接调用非静态方法。
Main
是一个静态函数。您需要首先实例化该类,然后使用该实例调用方法。第三,您可以直接从非静态方法调用静态方法。
First java will not allow you to have
do()
method. Instead you can make itdoOperation()
.Second You cann't invoke directly non static methods from static function.
Main
is a static function. You need to instantiate the class first and then invoke method using that instance.Third you can invoke static method directly from non static methods.