day04--方法_重载
This commit is contained in:
@@ -0,0 +1,46 @@
|
|||||||
|
package com.inmind.method01;
|
||||||
|
/**
|
||||||
|
* 方法重载:指在同一个类中,允许存在一个以上的同名方法,只要它们的参数列表不同即可,与修饰符和返回值类型无关。
|
||||||
|
* 方法重载:两同一不同
|
||||||
|
* 1.同一类中
|
||||||
|
* 2.方法名相同
|
||||||
|
* 3.参数列表不同(参数列表的个数,数据类型,参数的顺序)
|
||||||
|
*
|
||||||
|
* 总结:方法重载的作用:只要记住一个方法名,就可以调用不同的功能
|
||||||
|
*/
|
||||||
|
public class OverLoadDemo5 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//定义2个整数相加之和,并返回结果的方法
|
||||||
|
getSum(1, 2);
|
||||||
|
//定义3个整数相加之和,并返回结果的方法
|
||||||
|
getSum(1, 2, 3);
|
||||||
|
//定义4个整数相加之和,并返回结果的方法
|
||||||
|
getSum(1, 2, 3, 4);
|
||||||
|
|
||||||
|
System.out.println(1);
|
||||||
|
System.out.println(1.1);
|
||||||
|
System.out.println(true);
|
||||||
|
System.out.println("true");
|
||||||
|
System.out.println(1.5F);
|
||||||
|
System.out.println(1L);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getSum(int a,int b) {
|
||||||
|
int sum = a+b;
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static int getSum(int a,int b,int c) {
|
||||||
|
int sum = a+b+c;
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getSum(int a,int b,int c,int d) {
|
||||||
|
int sum = a+b+c+d;
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user