JAVA基础(17)---方法的重载

版权声明:如需转载请标明出处 https://blog.csdn.net/yj201711/article/details/83687717

方法的重载 

 如何来判断方法是否是重载的   

一同两不同:方法名称相同

                                参数列表不同(参数的个数不同,参数的类型不同,参数的顺序不同(针对参数列表中存在不同类型的参数))

                                返回值类型不同,不能界定方法是否重载

在一个类中,不能存在方法的定义形式完全一样的两个方法

public class FunctionDemo2{
	public static  void main(String[] args){
		
		FunctionDemo2 fun = new FunctionDemo2();
		int result = fun.add(2,3);
		System.out.println(result);
		int res = fun.add(1,2,3);
		System.out.println(res);
		double d = fun.add(2,3, 3.4);
		System.out.println(d);
		double d2 = fun.add(35.6,2,6);
		System.out.println(d2);
	}
	/*
		完成两个整数的加法运算
	*/
	public int add(int a , int b){
		int sum = a + b;
		return sum;
	
	}
	/*
	完成三个整数的加法运算
	*/
	public int  add(int a , int b ,int c){
		//int s = add2(a , b );

		return add(a , b ) + c;
	}
	public double add(int a , int b ,double c){
	
		return a + b + c;
		add(a,b)
	}
	public double add(double a ,int b ,int c){
	
	return a + b + c;
	}
	public int add(double a ,int b ,int c){
	
	return (int)(a + b + c);
	}
}

猜你喜欢

转载自blog.csdn.net/yj201711/article/details/83687717
今日推荐