081_方法重载练习

 1 package com_01;
 2     /*
 3         需求:
 4             使用方法重载的思想,设计比较两个整数是否相同的方法,兼容全整数类型(byte,short,int,long)
 5         思路:
 6             1.定义比较两个数字的是否相同的方法campare()方法,参数选择连个int型参数
 7             2.定义对应的重载方法,变更对应的参数类型,参数变更为两个long类型
 8             3.定义所有的重载方法,两个byte类型与两个short类型参数
 9             4.完成方法的调用,测试运行结果
10      */
11 public class MyMethod04 {
12         public static void main(String[] args) {
13             System.out.println(compare(10,20));
14             System.out.println(compare(10L,20L));
15             System.out.println(compare((byte)10,(byte)20));
16             System.out.println(compare((short)10,(short)20));
17         }
18         public static boolean compare(int a,int b){
19             System.out.println("int");
20             return a == b;
21         }
22         public static boolean compare(byte a,byte b){
23             System.out.println("byte");
24             return a == b;
25         }
26         public static boolean compare(short a,short b){
27             System.out.println("short");
28             return a == b;
29         }
30         public static boolean compare(long a,long b){
31             System.out.println("long");
32             return a == b;
33         }
34 }

 

posted @ 2020-11-29 20:43  找不到北的北  阅读(138)  评论(0)    收藏  举报