当前位置:网站首页>Ali II: can the main method be inherited?
Ali II: can the main method be inherited?
2022-04-22 03:06:00 【Second brother learns Java】
main Can methods be overloaded ?
- main Can methods be called by other methods ?
- main Can methods inherit ?

# Ali on both sides :main Can methods inherit ?
yesterday , A netizen in wechat group , I posted the process of interviewing Ali in the group . One of the interviews , He's in the group PUA Other netizens . This interview question is :Java Medium main Can methods inherit ?

We started to learn Java When it comes to programming , The first piece of code to run must be main Method ,main The format of the method is as follows :
public static void main(String[] args) {
}
that main What's special about the method ? Today, let's take a brief look at .
First, for main Method format definition :
「public」 :main The method is to start by JVM Loading ,public Is the highest accessible , So it needs to be declared as public;
「static」 : Method is called either through an object , Or through classes , and main Method, because it is called by the virtual machine , So there is no need to generate objects , Then declare as static that will do ;
「main」 : As for why the method name is main, I think it should refer to C The method name of the language ;
「void」 :main Method exit , There is no need to have a relevant return value , So it is void;
「String[]」 : This string array is used to accept user input parameters at runtime ; Because the string is in Java Is universal in , So using strings is the best choice ; Array words , Because we have more than one parameter , So the array must be appropriate ;
But from JDK1.5 After introducing dynamic parameters ,String[] Arrays can also be used String... args To achieve .
public static void main(String... args){
}
Except for the above JVM That's the rule main In addition to the special method , Other main Methods are no different from ordinary static methods .
# main Can methods be overloaded ?
This is OK , For example, we overload it with a method :
public class Main {
public static void main(String args) {
System.out.println("hello world:" + args);
}
public static void main(String[] args) {
main("test");
}
}
Compile operation , Obviously there's no problem , except JVM Specified as an application entry main Out of the way , Other main Methods are more common methods .
# main Can methods be called by other methods ?
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 Method execution :" + times);
if (times <= 0) {
System.exit(0);
}
main2(args);
}
}
Run the code , You can find that the code can execute normally :
main Method execution :3
main Method execution :2
main Method execution :1
main Method execution :0
So even as an application entry main Method , It can also be called by other methods , But pay attention to the closing mode of the program , Don't fall into a dead circle .
# main Can methods inherit ?
We've known before , When subclasses inherit , Subclasses can inherit the methods and variables of the parent class , So when the parent class defines main Method , And subclasses don't main The method is , That can inherit the parent class main Method , So as to run the program normally ?
public class Main {
public static void main(String[] args) {
System.out.println("hello world");
}
}
Defining subclasses :
public class Main2 extends Main {
}
At this time, we run subclasses Main2, You can find , Also printed hello world, This explanation main Methods can also be inherited . Then there is another hidden situation, which is also obvious , Human beings define their own main Method , Hide the implementation in the parent class , Then it's ok .
public class Main2 extends Main {
public static void main(String [] args) {
System.out.println("hello world Main2");
}
}
At this time, you will print out your own content :hello world Main2.
Look at it this way , except main Method is special as the entrance of application , In other cases, it is no different from the normal static method .
版权声明
本文为[Second brother learns Java]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211353162885.html
边栏推荐
- JS表格排序
- Day12 job
- Flutter03 dart asynchronous
- Sword finger offer special breakthrough version 92, flipped characters
- Comparison of hex, Base64 and URLEncode coding schemes
- Inline temporary variables for code refactoring
- 2022-4-21 operation
- 微信JSAPI支付方式与错误(当前页面的URL未注册,支付验证签名失败)
- [NCTF2019]Fake XML cookbook
- Buy a Tencent cloud server to play
猜你喜欢

kerberos認證協議

Text processing - sed

72. 编辑距离

嘉戎技術深交所上市破發:公司市值41億 應收賬款2.8億

Testng学习笔记

Detailed explanation of basic properties of binary sort tree

Niu Ke's entry record of C language

Programmers want to take root in Shenzhen. In addition to Tencent, they can also consider these companies

500 error, unable to forward after submitting the response

网络和多媒体知识(3)
随机推荐
Why is the country established
twenty-five. Module / built-in module / module installation
【经验】tf1.x迁移到tf2.x教程
Built in functions 123
二十五.模块/内置模块/模块的安装
二十七.包(import)
Modem dial-up playback, originally written in the millionaire, summarized.
别人写的叫博客,自己写的叫笔记
Use ordinary modem to make dial-up response. It was originally written in millionaire and summarized
class数组的类似常量定义
JS表格排序
嘉戎技术深交所上市破发:公司市值41亿 应收账款2.8亿
The interface needs to be forward compatible. If it is incompatible, open V2 interface
TestNG learning notes
In depth study of MySQL
Delphi自动适应屏幕大小
How to run collabora office in openshift
Word input formula shortcut key
The traffic lights made by enumeration have an interface
微信H5支付(报跨域问题)