48 lines
1.4 KiB
Java
48 lines
1.4 KiB
Java
package com.inmind.method;
|
|
/*
|
|
方法重载:指在同一个类中,允许存在一个以上的同名方法,只要它们的参数列表不同即可,与修饰符和返
|
|
回值类型无关。
|
|
|
|
方法重载:2同一不同
|
|
1.在同一个类中
|
|
2.方法名相同
|
|
3.参数列表不同,(与修饰符和返回值类型无关)(参数列表不同:参数列表中的个数,数据类型,参数的顺序)
|
|
|
|
重载的作用:只要记忆同一个方法名,就可以实现对应的功能
|
|
*/
|
|
public class Demo05 {
|
|
public static void main(String[] args) {
|
|
int a = 10;
|
|
int b = 10;
|
|
int c = 10;
|
|
int d = 10;
|
|
getSum(c,d);
|
|
getSum(a, b, c);
|
|
//请问,从第一天到今天,大家有没有遇到过方法重载呢????
|
|
System.out.println(1);
|
|
System.out.println(1.0);
|
|
System.out.println(true);
|
|
System.out.println('d');
|
|
System.out.println("嘿嘿");
|
|
|
|
}
|
|
//定义2个整数相加之和的方法,并返回和值
|
|
public static int getSum(int a,int b) {
|
|
int sum = a + b;
|
|
return sum;
|
|
}
|
|
|
|
//定义3个整数相加之和的方法,并返回和值
|
|
public static int getSum(int a,int b,int c) {
|
|
int sum = a + b + c;
|
|
return sum;
|
|
}
|
|
//定义4个整数相加之和的方法,并返回和值
|
|
public static int getSum(int a,int b,int c,int d) {
|
|
int sum = a + b + c + d;
|
|
return sum;
|
|
}
|
|
|
|
|
|
}
|