java6:方法重载(overload)

1 重载方法(oveloading methods)

The max method that was used earlier works only with the int data type. But what if you

need to determine which of two floating-point numbers has the maximum value? The solution

is to create another method with the same name but different parameters, as shown in the fol-
lowing code:
public static double max(double num1, double num2) {
if (num1 > num2)
return num1;
else
return num2;
}

在上一篇中,我们使用了max(int n1,int n2)来取两个int型的最大,但是如果我们需要比较的是两个double型呢?或者说,我们要比较的数有三个呢?这时候就可以用重载方法。这样,如果在你调用方法时候,传入的参数是int,那么max(int n1,init n2)就会被调用,如果传入的参数是double型的,那么max(double,double)的方法就会被调用,也就是是说,在方法调用时候,系统会先匹配下参数类型,个数来决定调用哪个方法,当然是调用最匹配的方法。

那么,当我们传入方法的参数是(int,double),这时候会成功调用函数吗?怎么调用呢?看下原文说法:

Can you invoke the max method with an int value and a double value, such as max(2,
2.5)? If so, which of the max methods is invoked? The answer to the first question is yes. The
answer to the second is that the max method for finding the maximum of two double values
is invoked. The argument value 2 is automatically converted into a double value and passed
to this method.
You may be wondering why the method max(double, double) is not invoked for the call
max(3, 4). Both  max(double, double) and max(int, int) are possible matches for
max(3, 4). The Java compiler finds the most specific method for a method invocation. Since
the method max(int, int) is more specific than max(double, double), max(int, int)
is used to invoke max(3, 4).

也就是说,答案是肯定也会调用方法的,但是他调用(double,double)的方法,其实觉得结合我们前边在讲到类型转换中就会明白,就像我们将一个int与一个double相加时候,int会被自动转为double,而当我们调用的(int,double)时候,java 编译器就找,如果能找到(int,double)的当然是最最匹配的,但是这时候找不到,只有(double,double)跟(int,int) ,当然是将int自动转为了double,然后调用double。同样,看官可以将(int,int)型的方法给注释掉,传入(3,5)的调用就没办法退而求有就调用(double,double )的

请看下面的例子,特别注意第四第五个调用,体会这种最匹配调用办法

public class TestOverLoading
{
	public static void main(String [] args)
	{
		System.out.println("the max of 3,5 is "+max(3,5));
		System.out.println("the max of 3.2,5.8 is "+max(3.2,5.8));
		System.out.println("the max of 3,9,7 is "+max(3,9,7));
		System.out.println("the max of 3,9.9 is "+max(3,9.9));
		System.out.println("the max of 3.0,9,7 is "+max(3.0,9,7));
	}
	public static int max(int n1,int n2)
	{
		System.out.print("max(int, int) is called, ");
		if(n1>n2) return n1;
		return n2;
	}
	public static double max(double n1,double n2)
	{
		System.out.print("max(double, double) is called, ");
		if(n1>n2) return n1;
		return n2;
	}
	public static int max(int n1,int n2,int n3)
	{
		System.out.print("max(int, int,int)is called, ");
		return (max(max(n1,n2),n3));
	}
		public static double max(double n1,double n2,double n3)
	{
		System.out.print("max(double, double,double)is called, ");
		double d=n1>n2?n1:n2;
		return d>n3?d:n3;
	}
}


Note
Overloaded methods must have different parameter lists. You cannot overload methods based on
different modifiers or return types.

注意 overloaded 方法必须在参数列表中有不同的。如果仅仅是修改返回值类型或者方法修饰符,是不能叫overload方法的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值