Files
javaSE251223/day04/src/com/inmind/Demo04_overload.java
2025-12-25 14:04:42 +08:00

82 lines
2.0 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;
/*
方法重载:指在同一个类中,允许存在一个以上的同名方法,只要它们的参数列表不同即可,与修饰符和返
回值类型无关。
方法重载2同一不同
1.在同一个类中
2.方法名相同
3.参数列表不同,(与修饰符和返回值类型无关)(参数列表不同:参数列表中的个数,数据类型,参数的顺序)
重载的作用:只要记忆同一个方法名,就可以实现对应多种的功能
---------------------------------------------------------------
class Demo
{
void show(int a,float b,char c)
{
}
}
下列哪些函数和给定函数重载了。
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 Demo04_overload {
void show(int a,float b,char c)
{
}
void show(int a,int b,int c){
}
double show(){
return 1.1;
}
void show(float b,int a,char c){
}
public static void main(String[] args) {
//定义2个整数相加之和的方法并返回和值
//定义3个整数相加之和的方法并返回和值
//定义4个整数相加之和的方法并返回和值
int sum = getSum(1, 2, 3);
System.out.println(sum);
System.out.println(1);
System.out.println(1.1);
System.out.println(true);
System.out.println("edd");
System.out.println('d');
}
//定义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;
}
}