Java main方法全解

1、main方法的重载

复制代码
 1 package cn.nxl2018;
 2 
 3 public class Main_test {
 4     public static void main(String args[]) {
 5         System.out.println("应用程序入口的main方法");
 6         main();
 7         main(10);
 8         main(10, 10);
 9     }
10     public static void main(){
11         System.out.println("无参的main方法");
12     }
13     public static void main(int i){
14         System.out.println("有参的重载main方法");
15     }
16     public static void main(int i,int j){
17         System.out.println("有两个参数的重载main方法");
18     }
19 }
复制代码

以上实例说明main方法是可以重载的,每个main方法的参数都是不同的,程序是可以运行的,可以输出应有的结果。但是,如果没有程序入口的main方法,只含有其他main方法。虽然编译此程序是可以的通过的,但是运行时会产生错误。这是因为程序运行时,Java虚拟机找不到对应的main方法,就会产生运行错误。

2、main方法的调用

复制代码
1 public class Main_test {                                        
2     public static void main(String[] args) {
3         main2(args);
4     }
5     public static void main2(String[] args){
6         main(args);
7     }
8 }
复制代码
运行程序后,程序无限递归执行两main方法,很明显main方法是可以被调用的。

3、main方法的继承

Main.java
1 public class Main{
2     public static void main(String[] args) {
3         System.out.println("Hello Word!");
4     }
5 }
Main_test.java
1 public class Main_test extends Main{
2 }
编译运行Main_test.java后输出了“Hello Word!”,说明main方法是可以被继承。

4、main方法的隐藏

Main.java
1 public class Main{
2     public static void main(String[] args) {
3         System.out.println("Main");
4     }
5 }
Main_test.java
1 public class Main_test extends Main{
2     public static void main(String[] args) {
3         System.out.println("Main_test");
4     }
5 }
http://www.vfv3740.top/
http://www.tbt7039.top/
http://www.wky3695.top/
http://www.kcs3342.top/
http://www.gum4900.top/
http://www.mrw5927.top/
http://www.wnu1861.top/
http://www.vlc4617.top/
http://www.idv6045.top/
http://www.jmk5203.top/
http://www.mug5965.top/
http://www.gtt6107.top/
http://www.cnp6436.top/
http://www.sdx1013.top/
http://www.jwd3113.top/
http://www.qeu2095.top/
http://www.tux4376.top/
http://www.tay3928.top/
http://www.tgq6935.top/
http://www.win4778.top/
http://www.ngh4321.top/
http://www.cqq1459.top/
http://www.fxm1291.top/
http://www.wyz5825.top/
http://www.kbx3827.top/
http://www.tqt8862.top/
http://www.yma5505.top/
http://www.rye6349.top/
http://www.xqs4260.top/
http://www.fjv3790.top/
http://www.wqv1289.top/
http://www.mxz6626.top/
http://www.npl2536.top/
http://www.vme0237.top/
http://www.edr0603.top/
http://www.kft8502.top/
http://www.kwb2561.top/
http://www.dqv1869.top/
http://www.iai9521.top/
http://www.jla2696.top/
http://www.vip1477.top/
http://www.ryc2010.top/
http://www.tpz5308.top/
http://www.gzi6562.top/
http://www.dwf8840.top/
http://www.soq5741.top/
http://www.pud6117.top/
http://www.zrv3166.top/
http://www.ylg5948.top/
http://www.evm5267.top/
http://www.mmc0244.top/
http://www.auk8793.top/
http://www.nbw1542.top/
http://www.oka2713.top/
http://www.txz2284.top/
http://www.amv5754.top/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值