2017-11-18 80 views
-1

我正在研究這個問題,不能這樣做,並不確定是否我甚至不明白什麼方法超載真的是。任何人都可以解釋它是什麼(因爲我在網上找到的資源不是很好),並且可能修復我所犯的錯誤?產品方法超載(這是上次詢問的同一個人)

的問題是:重載產品的方法,以允許其他類型的值相乘:

兩個雙打 一個int和雙 一個雙鍵和一個int 3整數 三項雙打

我收到錯誤說: - 可能有損從雙倍轉換爲int(第13,16,19) - 產品2 /產品3已被定義 - 方法期望類型int,int和類型int,int,int (第17行)

public class Product extends ConsoleProgram 
{ 
    public void run() 
    { 
     int intValue = 5; 
     double doubleValue = 2.5; 

     int product1 = product(intValue, intValue); 
     System.out.println(product1); 

     // Use method overloading to define methods 
     // for each of the following method calls 

     double product2 = product(doubleValue , doubleValue); 
     System.out.println(product1); 

     int product3 = product(intValue * intValue, intValue); 
     System.out.println(product1); 

     double product4 = product(intValue, doubleValue); 
     System.out.println(product1); 

     double product5 = product(doubleValue, intValue); 
     System.out.println(product1); 

     double product6 = product(doubleValue *doubleValue, doubleValue); 
     System.out.println(product1); 
    } 

    public int product(int one, int two) 
    { 
     return one * two; 
    } 

} 

謝謝你們

回答

3

方法重載只是當你想命名方法相同withing同一類,但簽名是不同的。

當我說的簽名不同,它可能是,參數類型是不同的,例如:

public int multiplyProduct(int one, int two) {} 

//We are overloading right here as we already defined a multiplyProduct above 

public int multiplyProduct(long one, long two){} 

或者它可以是參數只是數量不同:

public int multiplyProduct(long one, long two, long three){} 

在你上面的示例中您有一個類叫做產品:

class Product { 

    public int multiplyProduct(int one, int two) {} 

    public int multiplyProduct(int one, long two) {} 

    public int multiplyProduct(long one, long two){} 

    public int multiplyProduct(long one, long two, long three){} 

    public int multiplyProduct(double one, double two) {} 

} 

所以當你現在打電話給我thods你這樣做:

//To call the multiply(int one, int two) 
new Product().multiply(1,1) 

//To call the multiply(int one, long two) 
new Product().multiply(1,1L) 

//To call the multiply(long one, long two, long three) 
new Product().multiply(1L,1L,1L) 

//To call the multiply(double one, double two) 
new Product().multiply(1.0,1.0) 

有關錯誤的,你只需要創建一個接受兩個雙的新方法,並返回一個雙:

public void run() { 

    int intValue = 5; 
    double doubleValue = 2.5; 

    int product1 = product(intValue, intValue); 
    System.out.println(product1); 

    // Use method overloading to define methods 
    // for each of the following method calls 
    double product2 = product(doubleValue, doubleValue); 
    System.out.println(product1); 

    int product3 = product(intValue * intValue, intValue); 
    System.out.println(product1); 

    double product4 = product(intValue, doubleValue); 
    System.out.println(product1); 

    double product5 = product(doubleValue, intValue); 
    System.out.println(product1); 

    double product6 = product(doubleValue * doubleValue, doubleValue); 
    System.out.println(product1); 
} 

public int product(int one, int two) { 
    return one * two; 
} 

public double product(double one, double two) { 
    return one * two; 
} 
+0

所以基本上,如果我想重載INT產品1 =產品(的intValue,的intValue); ,我將不得不寫int product 1 = product(doubleValue,doubleValue)?? – CodingBat

+0

那種,我會編輯我的例子,以便通過一個例子來清楚! – Ian

+0

謝謝,你能解釋一下「從double到int的可能的有損轉換」嗎? double product2 = product(doubleValue,doubleValue);這是我寫的代碼,它向我展示了這個錯誤消息。 – CodingBat

1

方法重載只是當你想命名方法與同一班級相同,但簽名不同。 不同的簽名意味着或者參數是不同的類型或者 參數以不同的順序給出或者參數的數量是不同的。

定的代碼顯示了所有類型的方法重載的:

public class Product extends ConsoleProgram { 

    public void run(){ 

     int intValue = 5; 
     double doubleValue = 2.5; 

     int product1 = product(intValue, intValue); 
     System.out.println(product1); 

     // Use method overloading to define methods 
     // for each of the following method calls 

     double product1 = product(doubleValue, doubleValue); 
     System.out.println(product1); 

     int product3 = product(intValue, intValue, intValue); 
     System.out.println(product3); 

     double product2 = product(intValue, doubleValue); 
     System.out.println(product2); 

     double product2 = product(doubleValue, intValue); 
     System.out.println(product2); 

     double product3 = product(doubleValue, doubleValue, doubleValue); 
     System.out.println(product3); 


     public int product(int one, int two) 
     { 
      return one * two; 
     } 

     //different types of parameters 

     public double product(double one, double two) 
     { 
      return one * two; 
     } 

     //different number of parameters 

     public int product(int one, int two, int three) 
     { 
      return one * two * three; 
     } 

     public double product(int one, double two) 
     { 
      return one * two; 
     } 

     //different order of parameters 

     public double product(double one, int two) 
     { 
      return one * two; 
     } 

     public double product(double one, double two, double three) 
     { 
      return one * two * three; 
     } 
    } 
    } 

謝謝

+0

沒關係,所以我現在明白什麼是重載的方法,但我無法找到問題是與我的編碼..你能給我一個例子嗎?謝謝 – CodingBat

+0

行,所以看下面的代碼: –

+0

雙比爾(int item,int price){ double total = item * price;總回報: ; } double Bill(int item,double price){ double total = item * price;總回報: ; } /*我們通過改變參數的*/ 雙比爾(INT項,雙價,雙稅)的數量{ 雙總=項重載比爾功能 *價格*稅;總回報: ; } –

相關問題