Java:方法重载

2025-10-03 11:16:49

1、方法重载是两种多态的一种,我们可以向方法传递不同的消息,以便让对象根据相应的消息来产生相应的行为。

这里定义了三个方法。根据方法的类型,以及方法的参数列表和参数的数目来判断方法是否重载。

Java:方法重载

Java:方法重载

Java:方法重载

2、但这里需要注意的是,参数的名字不参与比较,所以在方法中,int a,和int b与int  m以及int n是没有区别的,如果这样定义程序会产生错误。

Java:方法重载

Java:方法重载

Java:方法重载

3、附代码:

class People{ float hello(int a ,int b){ return a+b; } float hello(long n,int m){ return a-b; } double hello(double a,int b){ return a*b; }}public class E17{ public static void main(String args[ ]){ People tom = new People(); System.out.println(tom.hello(10,20)); System.out.println(tom.hello(10L,20)); System.out.println(tom.hello(10.0,20)); }}

Java:方法重载

Java:方法重载

Java:方法重载

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢