我已经声明了三个具有相同名称“sum”的方法,目的是简单演示参数多态性的工作原理,但我不知道如何分别调用每个方法,这是我想要帮助的。我如何调用每种方法并知道我在调用哪个方法?
我尝试了一个简单System.out.println(sum(2,3));的返回 5 但如果我将其中一个数字设置在整数范围之外,我会收到错误消息:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The literal 3000000000 of type int is out of range
at codeTester.main.main(main.java:30)
/*
Author: Maxim Wilmot
Created: 18/04/2019
Polymorphism - Parametric
*/
package codeTester;
public class main {
//integer between -2 147 483 648 and 2 147 483 647
public static int sum(int a, int b) {
return a + b;
}
//decimal between -3,4.10^38 and 3,4.10^38
public static float sum(float a, float b) {
return a + b; //
}
//decimal between -1,7.10^308 and 1,7.10^308
public static double sum(double a, double b) {
return a + b;
}
public static void main(String args[]) {
System.out.println(sum(2,3)); //call a method, which one ?
}
}
我想要 3 个不同的输出,每种方法 1 个。那可能吗 ?可能为每种方法计算不同的数字?
谢谢你的帮助,马克斯。
慕尼黑的夜晚无繁华
相关分类