我们可以覆盖Java中的main()方法吗?
The question is that "Can we override main() method in Java?"
問題是“我們可以覆蓋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.
現在,問題是要提出為什么main()方法不能覆蓋的問題,所以我們將看到此問題的答案沒有被覆蓋,因為main()方法是靜態的,并且我們不能覆蓋靜態方法或換句話說,靜態方法不能被覆蓋。
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.
靜態方法是一個類方法,它不需要對象實例化,因此我們可以直接使用類名調用靜態方法。
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.
如果我們嘗試執行子類靜態方法,那么它將間接執行父類靜態方法,因此,在這種情況下,也沒有任何超越和壓倒繼承概念的感覺。
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.
假設如果我們將靜態main()方法保留在父類中,并且在子類中覆蓋相同的方法,并且如果我們調用子類main()方法,則默認情況下將調用父類方法,因此沒有覆蓋靜態方法的感覺這就是為什么main()方法不可替代,因為它是靜態的。
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.
靜態方法是一個類方法,因此該方法在同一類本身中的范圍,這就是為什么覆蓋概念不適用于類方法或換句話說靜態方法的原因。
The overriding concept is applicable for instance methods.
覆蓋概念適用于實例方法。
Example (Case1): We will see, in a java program to demonstrate main() method without overriding
示例(案例1):我們將看到,在一個Java程序中演示了main()方法而沒有覆蓋
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程序中看到通過覆蓋演示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
總結
以上是生活随笔為你收集整理的我们可以覆盖Java中的main()方法吗?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP array_pad()函数与示例
- 下一篇: java valueof_Java Sh