Java:方法重载

时间:2026-02-16 18:46:39

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:方法重载

© 2026 一点资料
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com