Java方法重载

描述

同一个 中,出现名称相同的多个方法,但是多个方法的形参列表是不同的,我们称为这些方法是重载方法。

题目

运用 Java 方法重载,分别定义形参列表 为不同 整数类型方法

题目解决思路

  1. Java 的整数类型分别为 byte、short、int、long。
  2. 定义 4 个名字一样,形参分别为 byte、short、int、long 的方法。
  3. 主方法调用这 4 个方法。

代码具体实现

public class OverrideDmeo01 { public static void main(String[] args) { System.out.println("嗨客网(www.haicoder.net)\n"); // 调用形参为 byte 的方法 compare((byte)1,(byte)1); // 调用形参为 short 的方法 compare((short)1,(short)1); // 调用形参为 int 的方法 compare(1,1); // 调用形参为 long 的方法 compare(1L,1L); } public static boolean compare(byte a , byte b){ System.out.println("方法形参为 byte"); return a == b; } public static boolean compare(short a , short b){ System.out.println("方法形参为 short"); return a == b ; } public static boolean compare(int a , int b){ System.out.println("方法形参为 int"); return a == b ; } public static boolean compare(long a , long b){ System.out.println("方法形参为 long"); return a == b ; } }

运行结果如下图:

14_java override.png

以上案例展示的就是 Java 方法重载。