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.java
E:\Programs>java WithoutOverridingMain
main() method can't override because this method is static
Example (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() method
public static void main(String[] args) {
// Display a message for the user
System.out.println("We are in Parent class main() method");
}
}
class Child extends Parent {
/* Overriding parent class main() method that's is not possible
It 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 class
Parent p = new Parent();
// Calling Parent class method
p.main(new String[0]);
// Creating Child class object
Child c = new Child();
// Call Child class method
c.main(new String[0]);
}
}
Output
输出量
E:\Programs>javac Main.java
E:\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