我们可以重载 Java 中的 main 方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3759315/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:31:16  来源:igfitidea点击:

Can we overload the main method in Java?

java

提问by Mohan

Can we overload a main()method in Java?

我们可以重载main()Java 中的方法吗?

采纳答案by Jon Skeet

You canoverload the main()method, but only public static void main(String[] args)will be used when your class is launched by the JVM. For example:

可以重载该main()方法,但只有public static void main(String[] args)在您的类由 JVM 启动时才会使用。例如:

public class Test {
    public static void main(String[] args) {
        System.out.println("main(String[] args)");
    }

    public static void main(String arg1) {
        System.out.println("main(String arg1)");
    }

    public static void main(String arg1, String arg2) {
        System.out.println("main(String arg1, String arg2)");
    }
}

That will alwaysprint main(String[] args)when you run java Test ...from the command line, even if you specify one or two command-line arguments.

当您从命令行运行时,即使您指定一两个命令行参数,它也会始终打印。main(String[] args)java Test ...

You can call the main()method yourself from code, of course - at which point the normal overloading rules will be applied.

当然,您可以main()自己从代码中调用该方法——此时将应用正常的重载规则。

EDIT: Note that you can use a varargs signature, as that's equivalent from a JVM standpoint:

编辑:请注意,您可以使用可变参数签名,因为从 JVM 的角度来看,这等效:

public static void main(String... args)

回答by JVM

Yes, you can overload main method in Java. But the program doesn't execute the overloaded main method when you run your program, you have to call the overloaded main method from the actual main method.

是的,您可以在 Java 中重载 main 方法。但是当你运行你的程序时,程序并没有执行重载的 main 方法,你必须从实际的 main 方法中调用重载的 main 方法。

that means main method acts as an entry point for the java interpreter to start the execute of the application. where as a loaded main need to be called from main.

这意味着 main 方法充当 java 解释器启动应用程序执行的入口点。其中加载的 main 需要从 main 调用。

回答by Shokat

Yes, by method overloading. You can have any number of main methods in a class by method overloading. Let's see the simple example:

是的,通过方法重载。通过方法重载,您可以在一个类中拥有任意数量的主要方法。让我们看一个简单的例子:

class Simple{  
  public static void main(int a){  
  System.out.println(a);  
  }  

  public static void main(String args[]){  
  System.out.println("main() method invoked");  
  main(10);  
  }  
}  

It will give the following output:

它将给出以下输出:

main() method invoked
10

回答by praveen

yes we can overload main method. main method must not be static main method.

是的,我们可以重载 main 方法。main 方法不能是静态的 main 方法。

回答by praveen

Yes, you can overload main method in Java. you have to call the overloaded main method from the actual main method.

是的,您可以在 Java 中重载 main 方法。您必须从实际的 main 方法中调用重载的 main 方法。

回答by vikram

Yes, main method can be overloaded. Overloaded main method has to be called from inside the "public static void main(String args[])" as this is the entry point when the class is launched by the JVM. Also overloaded main method can have any qualifier as a normal method have.

是的,main 方法可以重载。必须从“public static void main(String args[])”内部调用重载的 main 方法,因为这是 JVM 启动类时的入口点。重载的 main 方法也可以像普通方法一样具有任何限定符。

回答by Jaimin Patel

YES, you can overload main()

是的,您可以重载 main()

But to be clear -- although you can overload main, only the version with the standard signaturewill be executable as an application from the command line. e.g

但要明确——尽管您可以重载 main,但只有具有标准签名的版本才能作为应用程序从命令行执行。例如

public static void main(String[] args,int a){
// some code
}
2)public static void main(String[] args){//JVM will call this method to start 
// some code 
}

回答by Ankur Tewari

This is perfectly legal:

这是完全合法的:

public static void main(String[] args) {

}

public static void main(String argv) {
    System.out.println("hello");
}

回答by Subhash K U

Yes. 'main( )' method can be overloaded. I have tried to put in some piece of code to answer your question.

是的。'main()' 方法可以重载。我试图输入一些代码来回答你的问题。

public class Test{
static public void main( String [] args )
        {
                System.out.println( "In the JVMs static main" );
                main( 5, 6, 7 );    //Calling overloaded static main method
                Test t = new Test( );
                String [] message  = { "Subhash", "Loves", "Programming" };
                t.main(5);
                t.main( 6, message );
        }

        public static void main( int ... args )
        {
                System.out.println( "In the static main called by JVM's main" );
                for( int val : args )
                {
                        System.out.println( val );
                }
        }

        public void main( int x )
        {
                System.out.println( "1: In the overloaded  non-static main with int with value " + x );
        }

        public void main( int x, String [] args )
        {
                System.out.println( "2: In the overloaded  non-static main with int with value " + x );
                for ( String val : args )
                {
                        System.out.println( val );
                }
        }
}

Output:

输出:

$ java Test
In the JVMs static main
In the static main called by JVM's main
5
6
7
1: In the overloaded  non-static main with int with value 5
2: In the overloaded  non-static main with int with value 6
Subhash
Loves
Programming
$

In the above code, both static-method as well as a non-static version of main methods are overloaded for demonstration purpose. Note that, by writing JVMs main, I mean to say, it is the main method that JVM uses first to execute a program.

在上面的代码中,为了演示目的,静态方法和主方法的非静态版本都被重载。请注意,通过编写 JVM 的 main,我的意思是说,它是 JVM 首先用来执行程序的 main 方法。

回答by roottraveller

Yes, you can.

是的你可以。

The mainmethod in Java is no extra-terrestrial method. Apart from the fact that main()is just like any other method & can be overloaded in a similar manner, JVM always looks for the method signature to launch the program.

mainJava 中的方法不是外星方法。除了与main()任何其他方法一样并且可以以类似方式重载之外,JVM 始终寻找方法签名来启动程序。

  • The normal mainmethod acts as an entry point for the JVM to start the execution of program.

  • We can overload the mainmethod in Java. But the program doesn't
    execute the overloaded mainmethod when we run your program, we need to call the overloaded mainmethod from the actual main method only.

    // A Java program with overloaded main()
    import java.io.*;     
    public class Test {         
      // Normal main()
      public static void main(String[] args) {
        System.out.println("Hi Geek (from main)");
        Test.main("Geek");
      }     
      // Overloaded main methods
      public static void main(String arg1) {
        System.out.println("Hi, " + arg1);
        Test.main("Dear Geek","My Geek");
      }
      public static void main(String arg1, String arg2) {
        System.out.println("Hi, " + arg1 + ", " + arg2);
      }
    }
    

    Valid variants of main() in Java

  • 普通main方法作为JVM开始执行程序的入口点。

  • 我们可以main在 Java 中重载该方法。但是当我们运行你的程序时,程序并没有
    执行重载的main方法,我们只需main要从实际的 main 方法中调用重载的方法。

    // A Java program with overloaded main()
    import java.io.*;     
    public class Test {         
      // Normal main()
      public static void main(String[] args) {
        System.out.println("Hi Geek (from main)");
        Test.main("Geek");
      }     
      // Overloaded main methods
      public static void main(String arg1) {
        System.out.println("Hi, " + arg1);
        Test.main("Dear Geek","My Geek");
      }
      public static void main(String arg1, String arg2) {
        System.out.println("Hi, " + arg1 + ", " + arg2);
      }
    }
    

    Java 中 main() 的有效变体