java中可以从另一个类调用类的main()方法吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2550310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can a main() method of class be invoked from another class in java
提问by sa.
Can a main()
method of class be invoked in another class in java?
main()
java中可以在另一个类中调用类的方法吗?
e.g.
例如
class class1{
public static void main(String []args){
}
}
class class2{
public static void main(String []args){
class1.main();
}
}
回答by Gary
yes, but only if main is declared public
是的,但前提是 main 被声明为 public
回答by Dan Story
Sure. Here's a completely silly program that demonstrates calling main
recursively.
当然。这是一个完全愚蠢的程序,它演示了main
递归调用。
public class main
{
public static void main(String[] args)
{
for (int i = 0; i < args.length; ++i)
{
if (args[i] != "")
{
args[i] = "";
System.out.println((args.length - i) + " left");
main(args);
}
}
}
}
回答by mcobery
Yes as long as it is public and you pass the correct args. See this link for more information. http://www.codestyle.org/java/faq-CommandLine.shtml#mainhost
是的,只要它是公开的并且您传递了正确的参数。有关更多信息,请参阅此链接。 http://www.codestyle.org/java/faq-CommandLine.shtml#mainhost
回答by Binary Nerd
If you want to call the main method of another class you can do it this way assuming I understand the question.
如果你想调用另一个类的 main 方法,你可以这样做,假设我理解这个问题。
public class MyClass {
public static void main( String[] args ) {
System.out.println("main() method of MyClass");
OtherClass obj=new OtherClass();
}
}
class OtherClass {
public OtherClass() {
// Call the main() method of MyClass
String[] arguments = new String[] {"123"};
MyClass.main(arguments);
}
}
回答by Kamran Siddiqui
if I got your question correct...
如果我答对了你的问题...
main() method is defined in the class below...
main() 方法在下面的类中定义...
public class ToBeCalledClass{
public static void main (String args[ ]) {
System.out.println("I am being called");
}
}
you want to call this main method in another class.
你想在另一个类中调用这个 main 方法。
public class CallClass{
public void call(){
ToBeCalledClass.main(null);
}
}
回答by Muhammad Ashfaq
As far as I understand, the question is NOT about recursion. We can easily call main
method of another class in your class. Following example illustrates static and calling by object. Note omission of word static
in Class2
据我了解,问题与递归无关。我们可以很容易地main
在你的班级中调用另一个班级的方法。以下示例说明了静态和按对象调用。注意省略词static
inClass2
class Class1{
public static void main(String[] args) {
System.out.println("this is class 1");
}
}
class Class2{
public void main(String[] args) {
System.out.println("this is class 2");
}
}
class MyInvokerClass{
public static void main(String[] args) {
System.out.println("this is MyInvokerClass");
Class2 myClass2 = new Class2();
Class1.main(args);
myClass2.main(args);
}
}
Output Should be:
输出应该是:
this is wrapper class
this is class 1
this is class 2
这是包装类
这是 1 级
这是 2 级