我们可以覆盖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.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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544223.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一文读懂MySQL查询语句的执行过程

需要从数据库检索某些符合要求的数据,我们很容易写出 Select A B C FROM T WHERE ID XX 这样的SQL,那么当我们向数据库发送这样一个请求时,数据库到底做了什么?我们今天以MYSQL为例,揭示一下MySQL数据库的查询过程&a…

angularJS的$http.post请求,.net后台接收不到参数值的解决方案

JS通用部分var shoppingCartModule angular.module(starter, [ionic], function ($httpProvider) {// Use x-www-form-urlencoded Content-Type$httpProvider.defaults.headers.post[Content-Type] application/x-www-form-urlencoded;charsetutf-8;/*** The workhorse; conve…

带有示例的Python列表reverse()方法

列出reverse()方法 (List reverse() Method) reverse() method is used to reverse the elements of the list, the method is called with this list (list in which we have to reverse the elements) and it reverses all elements in the list. reverse()方法用于反转列表中…

复杂度O(n)倒转链表

1 public class ListNode {2 int val;3 ListNode next;4 ListNode(int x) { val x; }5 ListNode(){}6 7 public static ListNode revese(ListNode input)8 {9 ListNode head new ListNode();//头插法的头 10 ListNode cur in…

synchronized底层是如何实现的?

作者 | 磊哥来源 | Java面试真题解析(ID:aimianshi666)转载请联系授权(微信ID:GG_Stone)想了解 synchronized 是如何运行的?就要先搞清楚 synchronized 是如何实现?synchronized 同步…

java sublist_Java Vector subList()方法与示例

java sublist向量类subList()方法 (Vector Class subList() method) subList() method is available in java.util package. subList()方法在java.util包中可用。 subList() method is used to return a set of sublist [it returns all those elements exists in a given rang…

单例模式 4 种经典实现方法

0.前言 如果你去问一个写过几年代码的程序员用过哪些设计模式,我打赌,90%以上的回答里面会带【单例模式】。甚至有的面试官会直接问:说一下你用过哪些设计模式,单例就不用说了。你看,连面试官都听烦了,火爆…

CSRF简单介绍及利用方法-跨站请求伪造

0x00 简要介绍 CSRF(Cross-site request forgery)跨站请求伪造,由于目标站无token/referer限制,导致攻击者可以用户的身份完成操作达到各种目的。根据HTTP请求方式,CSRF利用方式可分为两种。 0x01 GET类型的CSRF 这种类…

java setsize_Java Vector setSize()方法与示例

java setsize向量类setSize()方法 (Vector Class setSize() method) setSize() method is available in java.util package. setSize()方法在java.util包中可用。 setSize() method is used to set the new size of this vector and when new size (n_size) > current size …

虾皮二面:什么是零拷贝?如何实现零拷贝?

前言 零拷贝是老生常谈的问题啦,大厂非常喜欢问。比如Kafka为什么快,RocketMQ为什么快等,都涉及到零拷贝知识点。最近技术讨论群几个伙伴分享了阿里、虾皮的面试真题,也都涉及到零拷贝。因此本文将跟大家一起来学习零拷贝原理。1.…

设计模式2:工程模式(1)

什么是工厂模式? 提供一个创建一系列或相互依赖对象的接口,而不需指定它们具体的类。 通俗的讲就是定义了多个产品的类,且只有一个工厂类,而这个工厂类根据需求的不同,可以产生不同产品类的对象。 作用:主要为创建对象提供过度接…

java indexof_Java Vector indexOf()方法与示例

java indexof向量类indexOf()方法 (Vector Class indexOf() method) Syntax: 句法: public int indexOf(Object ob);public int indexOf(Object ob, int indices);indexOf() method is available in java.util package. indexOf()方法在java.util包中可用。 indexO…

各大框架都在使用的Unsafe类,到底有多神奇?

前言 几乎每个使用 Java开发的工具、软件基础设施、高性能开发库都在底层使用了sun.misc.Unsafe,比如Netty、Cassandra、Hadoop、Kafka等。Unsafe类在提升Java运行效率,增强Java语言底层操作能力方面起了很大的作用。但Unsafe类在sun.misc包下&#xff0…

Codis 分布式缓存部署

为什么80%的码农都做不了架构师?>>> 环境介绍: 1:机器三台 ,IP/hostname 如下, hostname的设置很重要zookeeper / codis的通信都会用到,所以要配置好三台机器的hosts文件. 10.221.8.220 机器的hostname为 Redis1 10.221.8.221 机器的hostname为 Redis…

treeset java_Java TreeSet Higher()方法与示例

treeset javaTreeSet类Higher()方法 (TreeSet Class higher() method) higher() method is available in java.util package. Higher()方法在java.util包中可用。 higher() method is used to return the lowest element in this TreeSet that is higher than the specified el…

怎么解决MySQL死锁问题的?

咱们使用 MySQL 大概率上都会遇到死锁问题,这实在是个令人非常头痛的问题。本文将会对死锁进行相应介绍,对常见的死锁案例进行相关分析与探讨,以及如何去尽可能避免死锁给出一些建议。话不多说,开整!什么是死锁死锁是并…

strictmath_Java StrictMath cos()方法与示例

strictmathStrictMath类cos()方法 (StrictMath Class cos() method) cos() method is available in java.lang package. cos()方法在java.lang包中可用。 cos() method is used to return the trigonometric cosine of an angle of the given parameter in the method. Here, c…

Apache cxf JaxRs基本应用

2019独角兽企业重金招聘Python工程师标准>>> 在前一篇中&#xff0c;我们完成了《Apache cxf JaxWs基本应用》 的编写&#xff0c;我们现在实现一个Restful风格的Cxf 。 一、我们首先依旧是基于Maven project配置pom.xml的依赖 [html] view plaincopyprint? <pr…

白嫖1年阿里云,反手就搭一个Java环境

作者 | 磊哥来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;早上收到阿里云小姐姐的消息&#xff0c;阿里云有搞事情了&#xff0c;这次是送一年的阿里云 ECS 服务器。有便宜不占王八蛋…

setseed_Java Random setSeed()方法与示例

setseed随机类setSeed()方法 (Random Class setSeed() method) setSeed() method is available in java.util package. setSeed()方法在java.util包中可用。 setSeed() method is used to set the given seed of this Random Number Generator. setSeed()方法用于设置此随机数生…