我们可以覆盖Java中的main()方法吗?
The question is that "Can we override main() method in Java?"
問(wèn)題是“我們可以覆蓋Java中的main()方法嗎?”
No, we can't override the main() method in java.
不,我們不能覆蓋java中的main()方法 。
First, we will understand what is overriding? Overriding is what method signature will be the same in parent and child class and method body will be different in parent and child class.
首先,我們將了解什么是壓倒一切的? 父類和子類中的方法簽名將是相同的,父子類中的方法主體將是不同的。
Now, the question is to raise why main() method can't override so we will see the answer of this question main() method is not overridden because it is static and we can't override static methods or in other words static methods cannot be overridden.
現(xiàn)在,問(wèn)題是要提出為什么main()方法不能覆蓋的問(wèn)題,所以我們將看到此問(wèn)題的答案沒(méi)有被覆蓋,因?yàn)閙ain()方法是靜態(tài)的,并且我們不能覆蓋靜態(tài)方法或換句話說(shuō),靜態(tài)方法不能被覆蓋。
The static method is a class method, it does not need an object instantiation so we can call static methods directly with the class name.
靜態(tài)方法是一個(gè)類方法,它不需要對(duì)象實(shí)例化,因此我們可以直接使用類名調(diào)用靜態(tài)方法。
If we try to execute child class static method so it will indirectly parent class static methods will execute so, in that case, there is no sense of overriding and overwhelming the concept of inheritance too.
如果我們嘗試執(zhí)行子類靜態(tài)方法,那么它將間接執(zhí)行父類靜態(tài)方法,因此,在這種情況下,也沒(méi)有任何超越和壓倒繼承概念的感覺(jué)。
Let suppose if we keep static main() method in parent class and the same method override in child class and if we call child class main() method than by default parent class method will be called so there is no sense of overriding of static methods that's why main() method is not overridable because it is static.
假設(shè)如果我們將靜態(tài)main()方法保留在父類中,并且在子類中覆蓋相同的方法,并且如果我們調(diào)用子類main()方法,則默認(rèn)情況下將調(diào)用父類方法,因此沒(méi)有覆蓋靜態(tài)方法的感覺(jué)這就是為什么main()方法不可替代,因?yàn)樗庆o態(tài)的。
The static method is a class method so the scope of the method within the same class itself that's why the overriding concept is not applicable for class methods or in other words static methods.
靜態(tài)方法是一個(gè)類方法,因此該方法在同一類本身中的范圍,這就是為什么覆蓋概念不適用于類方法或換句話說(shuō)靜態(tài)方法的原因。
The overriding concept is applicable for instance methods.
覆蓋概念適用于實(shí)例方法。
Example (Case1): We will see, in a java program to demonstrate main() method without overriding
示例(案例1):我們將看到,在一個(gè)Java程序中演示了main()方法而沒(méi)有覆蓋
class WithoutOverridingMain {public static void main(String[] args) {System.out.println("main() method can't override");System.out.println("because this method is static");} }Output
輸出量
E:\Programs>javac WithoutOverridingMain.javaE:\Programs>java WithoutOverridingMain main() method can't override because this method is staticExample (Case2) : We will see in a java program to demonstrate main() method with overriding
示例(案例2):我們將在Java程序中看到通過(guò)覆蓋演示main()方法。
Note: It is not an overriding but seems to be overridden.
注意:這不是覆蓋,但似乎被覆蓋。
class Parent {// Parent class main() methodpublic static void main(String[] args) {// Display a message for the userSystem.out.println("We are in Parent class main() method");} }class Child extends Parent {/* Overriding parent class main() method that's is not possibleIt looks like overriding but it is just another main method with same signature of parent class*/public static void main(String[] args) {//Display a message for the user. System.out.println("We are in Child class main() method");} }class Main {public static void main(String[] args) {// creating an object of Parent classParent p = new Parent();// Calling Parent class methodp.main(new String[0]);// Creating Child class objectChild c = new Child();// Call Child class methodc.main(new String[0]);} }Output
輸出量
E:\Programs>javac Main.javaE:\Programs>java Main We are in Parent class main() method We are in Child class main() method翻譯自: https://www.includehelp.com/java/can-we-override-main()-method-in-java.aspx
總結(jié)
以上是生活随笔為你收集整理的我们可以覆盖Java中的main()方法吗?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: PHP array_pad()函数与示例
- 下一篇: css word-wrap_CSS中分词