2016-07-22 145 views
0

我對Java還是有點新,我可以使用這段代碼的一些幫助,到目前爲止,我編寫的方法和每種方法應該做的,但我真的不知道如何做超載效果,並使其工作如此我會很感激一個簡單的解釋。在Java中重載方法?

import java.util.Scanner; 
public class Assignment3 { 
static Scanner input = new Scanner(System.in); 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    myMethod(); 
} 
public static void myMethod(){ 
    System.out.println("Welcome to Java 1 "); 

} 
public static void myMethod(String msg, int counter){ 
    System.out.println("Enter your custom messege please: "); 
    msg = input.nextLine(); 

    System.out.println("Please enter how many times do you wish to print the messsege: "); 
    counter = input.nextInt(); 

    for (int i = 0; i <= counter; i++){ 
     System.out.println(msg); 
    } 
} 
public static void myMethod(int lowerLimit, int upperLimit){ 

    System.out.println("Please enter a lowerlimit: "); 
    lowerLimit = input.nextInt(); 

    System.out.println("Please enter an upperlimit: "); 
    upperLimit = input.nextInt(); 

    System.out.println("Press 1 for ascending order: "); 
    System.out.println("Press 2 for descending order: "); 
    System.out.println("Make your selection"); 
    int user1 = input.nextInt(); 
    System.out.println("How frequent do you wish the messege to be printed"); 
    int interval = input.nextInt(); 

    switch(user1){ 
    case 1: 
     for(int counter = lowerLimit; counter <= upperLimit; counter += interval){ 
      System.out.println(counter); 
     } 
     break; 
    case 2: 
     for(int counter = upperLimit; counter <= lowerLimit; counter -= interval){ 
      System.out.println(counter); 
     } 
     break; 
     default : 
      System.out.println("Something went wrong !!!"); 
    } 

} 
public static void myMethod(double number1, double number2){ 

    number1 = (Math.random() * 100); 
    number2 = (Math.random() * 100); 
    double product = (number1 * number2); 

    System.out.println("The product of " + number1 + " and " + number2 + " is " + product); 
} 
] 

回答

0

在回答您的評論:

你只需要調用其他兩個「myMethod的」在你的主,用他們各自的簽名:

public static void main(String[] args) { 
     // Call without argument 
     myMethod(); 

     // Call with String and integer 
     myMethod("test", 42); 

     // Call with Integer and Integer 
     myMethod(42, 666); 
    } 

然後會調用正確的。這回答了你的問題了嗎 ?

+0

非常感謝你,這確實有助於解釋和修復代碼,我想我需要更多的練習和挖掘來完善它。 再次謝謝大家 – Angelos

1

您的myMethod方法已經過載。重載方法只是一種可以接受兩個或更多不同參數集的方法。 (見https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

例如:

public void foo(int a) { 
    System.out.println("Printing out the int " + a); 
} 

public void foo(double a) { 
    System.out.println("Printing out the double " + a); 
} 

富有兩個可能的參數集,一個接受一個int和一個接受一個double。現在,如果你這樣做:

int a = 10; 
double b = 10.5; 

foo(a); 
foo(b); 

這將返回:

Printing out the int 10 
Printing out the double 10.5 
+0

我想補充一點,這適用於_static_和_instance_方法(OP有靜態方法)。還提供來自Oracle文檔或Java Lang Spec的鏈接。 –

+0

謝謝您的迴應,但我想我沒有正確解釋我的問題。 此代碼缺少負責通過函數調用方法的部分,如我不知道如何告訴編譯器調用負責特定函數的方法,所有我能夠從中獲得試圖運行它的代碼是這樣的消息:歡迎來到java 1 – Angelos

+0

那是因爲它是在你的main方法中被調用的唯一方法:myMethod(); !其他方法從未被稱爲 – Tim

0

上述職位有你的答案,你myMethod的方法已經超負荷運轉,但方法重載是一種功能,允許類有兩個或多個具有相同名稱的方法,如果它們的參數列表不同。 你有你的方法,接受不同的參數具有不同數據類型