阿里二面:main 方法可以继承吗?
点击上方“码农突围”,马上关注
这里是码农充电第一站,回复“666”,获取一份专属大礼包
真爱,请设置“星标”或点个“在看”
main方法能重载么? main方法能被其他方法调用么? main方法可以继承么?
Java 中的 main 方法可以继承吗?
public static void main(String[] args) {
}
String[]
数组也可以使用String... args
来实现。public static void main(String... args){
}
main方法能重载么?
public class Main {
public static void main(String args) {
System.out.println(
"hello world:" + args);
}
public static void main(String[] args) {
main(
"test");
}
}
main方法能被其他方法调用么?
public class Main {
private static int
times = 3;
public static void main2(String[] args) {
times--;
main(args);
}
public static void main(String[] args) {
System.out.println(
"main方法执行:" +
times);
if (
times <= 0) {
System.exit(0);
}
main2(args);
}
}
main方法执行:3
main方法执行:2
main方法执行:1
main方法执行:0
main方法可以继承么?
public class Main {
public static void main(String[] args) {
System.out.println(
"hello world");
}
}
public class Main2 extends Main {
}
hello world
,这说明 main 方法也是可以继承的。那么还有一种隐藏的情况也很显然了,子类定义自己的 main 方法,隐藏掉父类中的实现,那么这也是可以的。public class Main2 extends Main {
public static void main(String [] args) {
System.out.println(
"hello world Main2");
}
}
hello world Main2
。main
方法作为应用程序的入口比较特殊外,其他情况下与正常的静态方法是没什么区别的。(完)
码农突围资料链接
1、卧槽!字节跳动《算法中文手册》火了,完整版 PDF 开放下载!
2、计算机基础知识总结与操作系统 PDF 下载
3、艾玛,终于来了!《LeetCode Java版题解》.PDF
4、Github 10K+,《LeetCode刷题C/C++版答案》出炉.PDF
2、计算机基础知识总结与操作系统 PDF 下载
3、艾玛,终于来了!《LeetCode Java版题解》.PDF
4、Github 10K+,《LeetCode刷题C/C++版答案》出炉.PDF
欢迎添加鱼哥个人微信:smartfish2020,进粉丝群或围观朋友圈。
关键词
代码
main方法
父类
数组
参数
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。