2013-07-13 7 views
0

나는 주에있는 방법 "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에 대해서만 오류가 발생하지만 실제로는 오류가 발생하지 않습니다. 왜 이런거야?

+0

난 당신이'새로운 OverloadedMethod()를 의미하는 생각,' –

+0

어이가 같은 X 같은 인스턴스를 생성'OverloadedMethod X = newOverloadedMethod는()'다음'X와 메소드를 호출 '인스턴스는'x.displayInformation (19,90)' –

+0

같은 오류가 발생합니다 –

답변

0

OverloadedMethod은 클래스이며 매개 변수가 정의 된 생성자가 없습니다.

그래서 당신은

OverloadedMethod o= new OverloadedMethod(); 


    switch (task){ 
    case 1: 
    System.out.println(o.displayInformation(num1,num2));break; 
    case 2: 
    System.out.println(o.displayInformation(str,num));break; 
    case 3: 
    System.out.println(o.displayinformation(str1,str2));break; 
    default: 
     System.out.println("invalid option");  

을 할 필요가 그리고 당신은, displayInfomartionObject (또는 서브 클래스)를 반환해야 정의하고 num1num2strnumstrstr2

그리고 이하 중요한에 값을 할당 할 필요가 인간을 위해 읽을 수 있도록 구현 된 toString()

예 :

public String displayInformation(...)

+0

displayInformation 메서드에서 num1 num2 str num str 및 str2에 대한 값을 입력하도록 사용자에게 요청하고이를 묻습니다. 메인에서 정의해야합니까? –

+0

@javalava 물론, 어딘가에 존재해야합니다. – nachokk

+0

'displayInformation'은 인쇄물을 반환해야합니다. –

관련 문제