重载的概念
简单地说,在同一个类中,方法名相同而参数序列不同的方法称为重载.
注意!
注意:方法的重载与方法的返回值类型无关!
参数序列表示参数的个数和参数的数据类型,不包含参数名
操作实例
package com.hnumi.java.chapter03;
public class Demo01_方法的重载 {
public static void main(String[] args) {
}
//方法1:其他方法与此进行对比
public static int sum(int a,int b){
System.out.println("int sum(int,int)");
return a + b;
}
//方法2:参数的类型不同,因此是方法1的重载
public static int sum(short a,short b){
System.out.println("int sum(short,short)");
return a + b;
}
//方法3:参数的个数不同,因此是方法1 的重载
public static int sum(short a,short b,int c){
System.out.println("int sum(short,short,int)");
return a + b + c;
}
//方法4:方法名不同,因此不是重载
public static int sum1(int a,int b){
System.out.println("int sum(int,int)");
return a + b;
}
//方法5:法名相同,参数序列也相同,因此不是重载
/*public static short sum(short a,short b){
System.out.println("int sum(short,short)");
return (short) (a + b);
}*/
}
操作实例简单明了,适合新手入门.