JavaSE-5.2【方法重载案例】

 1 package day5;
 2 
 3 /**
 4  * @author haifei
 5  *
 6  * 方法重载案例
 7  * 使用方法重载的思想,设计比较两个整数是否相同的方法,兼容全整数类型(byte,short,int,long)
 8  *
 9  */
10 public class demo2 {
11     public static void main(String[] args) {
12         System.out.println(compare(10, 20));
13         System.out.println(compare(10L, 20L));
14         System.out.println(compare((short) 10, (short) 20));
15         System.out.println(compare((byte) 10, (byte) 20));
16     }
17 
18     public static boolean compare(int a, int b){
19         return a == b;
20     }
21 
22     public static boolean compare(long a, long b){
23         return a == b;
24     }
25 
26     public static boolean compare(short a, short b){
27         return a == b;
28     }
29 
30     public static boolean compare(byte a, byte b){
31         return a == b;
32     }
33 }

 

posted @ 2021-05-21 14:32  yub4by  阅读(88)  评论(0编辑  收藏  举报