如何在java中调用重载方法?

时间:2013-07-13 03:38:37

标签: java methods overloading

我无法在main中调用方法“displayInformation”:

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或int& string或string& string。我在调用main中的方法时遇到了麻烦,我不断收到错误“错误:'。class''对于我创建对象的行...我只得到varibales o和oo但不是ooo的错误。这是为什么?

1 个答案:

答案 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");     

您需要为num1 num2 str num strstr2

定义和分配值

同样重要的是,displayInfomartion必须返回Object(或子类),toString()已实现为人类可读。

示例:

public String displayInformation(...)