我已經聲明了三個具有相同名稱“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 WilmotCreated: 18/04/2019Polymorphism - 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 個。那可能嗎 ?可能為每種方法計算不同的數字?謝謝你的幫助,馬克斯。
如何在 Java 中打印不同的參數多態方法?
慕碼人2483693
2022-10-12 15:40:40