Files
javaSE-0113/day04/src/com/inmind/overload02/Demo04.java
2026-01-16 15:58:52 +08:00

35 lines
1.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}