day04-方法_重载

This commit is contained in:
2026-04-25 16:26:45 +08:00
parent 4beb65b4c4
commit ae32de5899
2 changed files with 81 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
package com.inmind.overload02;
/**
* 方法重载:指在同一个类中,允许存在一个以上的同名方法,只要它们的参数列表不同即可,与修饰符和返回值类型无关。
* 方法重载:两同一不同
* 1.同一类中
* 2.方法名相同
* 3.参数列表不同(参数列表的个数,数据类型,参数的顺序)
*
* 总结:方法重载的作用:只要记住一个方法名,就可以调用不同的功能
*/
public class Demo04 {
public static void main(String[] args) {
//定义2个整数的相加之和并返回结果
getSum(10, 20);
//定义3个整数的相加之和并返回结果
getSum(10, 20,1);
//定义4个整数的相加之和并返回结果
getSum(10, 20,1,4);
System.out.println(1);
}
//定义2个整数的相加之和并返回结果
public static int getSum(int a,int b){
int sum = a + b;
return sum;
}
//定义23个整数的相加之和并返回结果
public static int getSum(int a,int b,int c){
int sum = a + b + c;
return sum;
}
//定义23个整数的相加之和并返回结果
public static int getSum(int a,int b,int c,int d){
int sum = a + b + c + d;
return sum;
}
}

View File

@@ -0,0 +1,40 @@
package com.inmind.overload02;
/*
下列哪些方法和指定的方法重载了。
a.int show(int x,float y,char z)
b.void show(float b,int a,char c)
c.void show(int c,float a,char b)
d.void show(int a,int b,int c)
e.double show()
*/
public class Test05 {
void show(int a,float b,char c)
{
}
double show(){
return 1.0;
}
void show(int a,int b,int c){
}
/*void show(int c,float a,char b){
}*/
void show(float b,int a,char c){
}
/*int show(int x,float y,char z){
return 1;
}*/
}