day04-方法重载的概念
This commit is contained in:
81
day04/src/com/inmind/Demo04_overload.java
Normal file
81
day04/src/com/inmind/Demo04_overload.java
Normal file
@@ -0,0 +1,81 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user