Java方法重载的区分


测试程序代码:

 
 1   class Computer {
 2     private int x;
 3 
 4     public Computer(){
 5         this(10);
 6     }
 7 
 8     /**
 9      * 构造方法重载
10      * @param x
11      */
12     public Computer(int x){
13         this.x=x;
14     }
15     /**
16      * 根据参数个数不同的方法重载
17      * @param a
18      * @param b
19      * @return
20      */
21     public  int max(int a,int b){
22         return max(a,b,Integer.MIN_VALUE);//Integer.MIN_VALUE是int类型中最小数-2147483648
23     }
24     public  int max(int a,int b,int c){
25         int max=a;
26         if (max<b) max=b;
27         if (max<c) max=c;
28         return max;
29     }
30     /**
31      * 根据参数类型不同方法(函数)重载
32      * @param x
33      * @return
34      */
35     public int add(int x){
36         return x;
37     }
38     public float add(float x){
39         return x;
40     }
41 
42     /**
43      * 测试方法的返回值是否可以作为重载的标准
44      * @return
45      */
46 //    public int returnValue(){
47 //        return 5;
48 //    }
49 //    public float returnValue(){
50 //        return 5;
51 //    }
52 //    编译器报错,returnValue方法已经存在。很明显根据返回值类型是不可以判断方法是否重载的。
53 }
54 class OverLoading{
55     public void test(){
56         Computer computer=new Computer();
57         new Computer(100);
58         int max_2=computer.max(10,11);
59         int max_3=computer.max(10,29,33);
60         int tmp1=computer.add(10);
61         float tmp2=computer.add(10f);
62     }
63 }


 

结论:判断方法(函数)重载的依据是参数个数的不同和参数类型的不同,根据返回值类型的不同是不可以判断方法重载。

关联博客(CSDN):https://blog.csdn.net/m0_38022608/article/details/80251993

欢迎私下交流编程技术!(QQ:2187093468)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM