java方法重载和重载方法_我们可以在Java中重载main()方法吗?
java方法重載和重載方法
The question is that "can we overload main() method in Java?"
問題是“我們可以在Java中重載main()方法嗎?”
Yes, We can overload the main() method in Java.
是的,我們可以重載Java中的main()方法 。
JVM calls any method by its signature or in other words JVM looks signature and then call the method.
JVM通過其簽名調用任何方法,換句話說,JVM查找簽名,然后調用該方法。
If we overload a main() method in a program then there will be multiple main() methods in a program. So JVM calls which method? we don't need to confuse if we have multiple main() methods then JVM calls only one main() method with (string[] argument) by default.
如果我們在程序中重載main()方法 ,則程序中將有多個main()方法 。 那么JVM調用哪個方法? 如果我們有多個main()方法,則無需混淆,然后JVM默認僅使用(string []參數)調用一個main()方法 。
Example:
例:
class MainMethodOverloading {public static void main(String[] args) {System.out.println("We are in String[] args");}public static void main(int args) {System.out.println("We are in int args");}public static void main(String args) {System.out.println("We are in String args");} }Output
輸出量
E:\Programs>javac MainMethodOverloading.javaE:\Programs>java MainMethodOverloading We are in String[] argsBy default JVM call only one main() method of a String argument, But if we want to call another main() method or any other overloaded main() method, then we can do only one thing that is we can call overloaded main() method explicitly.
默認情況下,JVM僅調用String參數的一個main()方法 ,但是如果我們要調用另一個main()方法或任何其他重載的main()方法 ,那么我們只能做一件事,即可以調用重載的main() )方法 。
We can call other main() methods inside the original main() method with a String argument.
我們可以使用String參數在原始main()方法內調用其他main()方法 。
Example:
例:
// Java Program to demonstrate overloading of // main() method import java.io.*;class MainMethodOverloading {// Origional main() methodpublic static void main(String[] args) {System.out.println("Hi, We are in main (String [] args) ");MainMethodOverloading.main("Call main() with one argument");}// These are the overloaded main() methods public static void main(String args1) {System.out.println(args1);MainMethodOverloading.main("call main() with", "two argument");}public static void main(String args1, String args2) {System.out.println(args1 + args2);MainMethodOverloading.main("call main() with", "three argument", "from two argument main()");}public static void main(String args1, String args2, String args3) {System.out.println(args1 + args2 + args3);} }Output
輸出量
E:\Programs>javac MainMethodOverloading.javaE:\Programs>java MainMethodOverloading Hi, We are in main (String [] args) Call main() with one argument call main() withtwo argument call main() withthree argumentfrom two argument main()翻譯自: https://www.includehelp.com/java/can-we-overload-main-method-in-java.aspx
java方法重載和重載方法
總結
以上是生活随笔為你收集整理的java方法重载和重载方法_我们可以在Java中重载main()方法吗?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ruby array_Ruby中带有示例
- 下一篇: c ++向量库_在C ++中对2D向量进