简体   繁体   中英

how to call overloaded method in java?

I am having trouble trying to call the methods "displayInformation" in main:

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);
      }
}

In the program. I ask the user to choose a task, and each task calls a different "displayInformation" method to ask for either an int&int or int&string or string&string. I am having trouble calling the methods in main though, I keep getting the error "error: '.class' expected" for the lines where I create the object..I get errors only for varibales o and oo but not ooo. Why is this?

OverloadedMethod is a class and don't have constructor with parameters defined.

So you have to do

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

And you need to define and assign values to num1 num2 str num str and str2

And not less important, displayInfomartion must return Object (or subclasses) that have toString() implemented to be readable for humans.

Example :

public String displayInformation(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM