Demo entry 6869281

陈杰海

   

Submitted by anonymous on Aug 20, 2019 at 12:12
Language: Java. Code size: 825 Bytes.

public class Test2{
	public static void main(String[]args){
		System.out.println(add(3,5)); 
		System.out.println(add(3,5,10));
		System.out.println(add(3.0,5));
		//我们已经见过的重载
		System.out.println();//0个参数
		System.out.println(1);//参数是1个int
		System.out.println(3.0);//参数是1个double
	}
	/*求和的方法*/
	public static int add(int n1,int n2){
		int sum =n1+n2;
		return sum;
	}
	//方法名相同,参数个数不同,构成重载
	public static int add(int n1,int n2,int n3){
		int sum =n1+n2+n3;
		return sum;
	}
	//方法名相同,参数类型不同,构成重载
	public static double add(double n1,int n2){
		double sum=n1+n2;
		return sum;
	}
	/*编译错误:只有返回值不同,不构成方法的重载
	public static double add(int n1,int n2){
		double sum =n1+n2;
		retrun sum;
	}
	//编译错误:参数顺序不同,不构成方法的重载
	public static int add(int n2,int n1){
		double sum = n1+n2;
		return sum;
	}
	*/
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).