zoukankan      html  css  js  c++  java
  • Java之方法重载篇(我重载了,你要如何来调用我。。)




    # **一、课前引言**





    • 请看一下代码,你发现什么特殊之处了吗?

        public class MethodOverload {
            public static void main(String[] args) {
                System.out.println("The square of integer 7 is " + square(7));
                System.out.println("
      The square of double 7.5 is " + square(7.5));
            }
            public static int square(int x) {
                return x * x;
            }
            public static double square(double y) {
                return y * y;
            }
        }
      




    二、如何重载?如何调用?






          上述示例代码展示了Java的“方法重载(overload)”特性。

    1. 重载的条件

    • 两个或多个方法才能构成“重载”关系;
    • 方法名一致;
    • 参数类型不同,参数个数不同,或者是参数类型的顺序不同;
    1. 需要注意

    方法的返回值不作为方法重载的判断条件。

    1. 查看JDK文档中System.out.println()方法,发现了什么?

       共有10个println()方法构成了“重载关系”,如下图:

    System.out.println()
    4. ## 如何调用?    由课前引言中的示例可知,调用具有“重载关系”的方法时,传递的实参类型决定了所调用的方法。    当实参类型是整型,那么系统会相应的调用形参同样是整型的int square(int x)方法;当实参类型是浮点型,那么系统会相应的调用形参同样是整型的int square(double y)方法;    方法调用跟形参变量名(x,y)没有关系,与形参类型有关。
查看全文
  • 相关阅读:
    204. Count Primes (Integer)
    203. Remove Linked List Elements (List)
    202. Happy Number (INT)
    201. Bitwise AND of Numbers Range (Bit)
    200. Number of Islands (Graph)
    199. Binary Tree Right Side View (Tree, Stack)
    198. House Robber(Array; DP)
    191. Number of 1 Bits (Int; Bit)
    190. Reverse Bits (Int; Bit)
    189. Rotate Array(Array)
  • 原文地址:https://www.cnblogs.com/lxmwb/p/J2SE.html
  • Copyright © 2011-2022 走看看