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