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.java

E:\Programs>java MainMethodOverloading
We are in String[] args

By 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() method
    public 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.java

E:\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方法重载和重载方法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值