java中存在两种多态,重载和重写,后者是与继承有关的多态,这里不再赘述。而前者则是指一个类中可以有多个方法具有相同的名字,但这些方法的参数必须不同。这里需要注意的是参数不同需要满足2个条件,一个是参数的个数不同,一个是参数个数相同,但参数列表中对应的某个参数的类型不同。
工具/原料
类,方法,参数列表
方法/步骤
2
但这里需要注意的是,参数的名字不参与比较,所以在方法中,int a,和int b与int m以及int n是没有区别的,如果这样定义程序会产生错误。
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)); }}
注意事项
参数的名字不参与比较,类如n,m和a,b
上一篇:黑樱桃巧克力蛋糕卷的做法
下一篇:Java的方法重载