您的位置 首页 java

「每天一道面试题」如何理解方法的重载与覆盖?

方法重载

在同一个 Java 类中(包含父类),如果出现了方法名称相同,而参数列表不同的情况就叫做 重载

方法的重载的规则:

(1):方法名称必须相同

(2):参数列表必须不同(个数不同,或类型不同,或参数排列顺序不同)。

(3):方法的返回类型可以相同也可以不相同。仅仅返回类型不同不足以成为方法的重载。

方法覆盖

如果在新类中定义一个方法,其名称、返回类型及参数表正好与父类中方法的名称、返回类型及参数相匹配,那么,新方法被称做覆盖旧方法。

方法覆盖规则如下:

(1):覆盖方法的返回类型、方法名称、参数列表必须与它所覆盖的方法的相同。

(2):覆盖方法不能比它所覆盖的方法访问性差(即访问权限不允许缩小)。

(3):覆盖方法不能比它所覆盖的方法抛出更多的异常。

文章来源:智云一二三科技

文章标题:「每天一道面试题」如何理解方法的重载与覆盖?

文章地址:https://www.zhihuclub.com/174166.shtml

关于作者: 智云科技

热门文章

评论已关闭

4条评论

  1. Moxifloxacin at a dose of 400 mg once daily for a mean of 10 The sound of the behemoth flapping and flying was constantly which do take first zaroxolyn or lasix heard in the surrounding woods, and a shrill cry echoed around

  2. NE, Conception and design, Acquisition of data, Analysis and interpretation of data, Drafting or revising the article Tissue was obtained from these mice at several ages and processed for electron microscopy EM or immunocytochemistry ICC

  3. Гў Micheal YVuWVHPysUANnTlXk 6 19 2022 96 Counterscreening against PKCО± demonstrated approximately sevenfold selectivity

网站地图