나는 주에있는 방법 "displayInformation"을 호출하려고하는 데 문제가 있어요 : 프로그램에서Java에서 오버로드 된 메서드를 호출하는 방법?
import java.util.Scanner;
public class overload
{
public static void main (String[ ] args)
{
Scanner keyboard = new Scanner(System.in);
int task;
System.out.println("Select task 1-3: ");
task=keyboard.nextInt();
if (task==1)
{
OverloadedMethod o= new OverloadedMethod();
System.out.println(o.displayInformation(int,int));
}
else if (task==2)
{
OverloadedMethod oo= new OverloadedMethod();
System.out.println(oo.displayInformation(String, int));
}
else if (task==3)
{
OverloadedMethod ooo= new OverloadedMethod();
System.out.println(ooo.displayinformation(String,String));
}
}
}
class OverloadedMethod
{
public void displayInformation (int num1, int num2)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your first int value: ");
num1=keyboard.nextInt();
System.out.print("Please enter your second int value: ");
num2=keyboard.nextInt();
System.out.print("Values entered: " + num1 + " and " + num2);
}
public void displayInformation (String str, int num)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your first string value: ");
str=keyboard.nextLine();
System.out.print("Please enter your second int value: ");
num=keyboard.nextInt();
System.out.print("Values entered: " + str + " and " + num);
}
public void displayInformation(String str1, String str2)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your first string value: ");
str1=keyboard.nextLine();
System.out.print("Please enter your second string value: ");
str2=keyboard.nextLine();
System.out.print("Values entered: " + str1 + " and " + str2);
}
}
합니다. 사용자에게 작업을 선택하도록 요청하고 각 작업은 다른 "displayInformation"메서드를 호출하여 & int 또는 int & 문자열 또는 & 문자열을 요청합니다. 그래도 main에서 메소드를 호출하는 데 문제가 있습니다. "error : '.class'객체를 생성하는 행에 'expected'가 있습니다. varibales o 및 oo에 대해서만 오류가 발생하지만 실제로는 오류가 발생하지 않습니다. 왜 이런거야?
난 당신이'새로운 OverloadedMethod()를 의미하는 생각,' –
어이가 같은 X 같은 인스턴스를 생성'OverloadedMethod X = newOverloadedMethod는()'다음'X와 메소드를 호출 '인스턴스는'x.displayInformation (19,90)' –
같은 오류가 발생합니다 –