正在回答
2回答
我说的是与返回值无关,决定重载是方法名一样,参数不一样;上面的例子我把返回值去掉一样不影响:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | //返回值版本 public class Test { public int plus( int m, int n){ return m+n; } public double plus( double m, double n){ return m+n; } public int plus( int [] arr){ int sum= 0 ; for ( int i= 0 ;i<arr.length;i++){ sum=sum+arr[i]; } return sum; } public static void main(String[] args) { Test test = new Test(); test.plus( 1 , 2 ); test.plus( 3.0 , 4.2 ); test.plus( new int []{ 1 , 2 , 3 , 4 , 5 }); } } |
无返回值版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | //无返回值版本 public class Test { public void plus( int m, int n){ System.out.println(m+n); } public void plus( double m, double n){ System.out.println(m+n);; } public void plus( int [] arr){ int sum= 0 ; for ( int i= 0 ;i<arr.length;i++){ sum=sum+arr[i]; } System.out.println(sum); } public static void main(String[] args) { Test test = new Test(); test.plus( 1 , 2 ); test.plus( 3.0 , 4.2 ); test.plus( new int []{ 1 , 2 , 3 , 4 , 5 }); } } |
walkingway
2018-02-07 13:11:19
只有方法名相同,参数列表不同的才是重载,返回值不在考虑范围内
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧