简体   繁体   中英

Using a variable from the driver class in another class' method (java)

I'm writing my first program in Java with an object-oriented approach. So far I've been learning to program in Java with a sequential approach, but the switch to object-oriented has given me a few problems.

Firstly, my program is a simple program that makes a virtual dog perform some tricks. I have a dog class and a dogDriver class. In my dogDriver class I have the following code fragment:

System.out.println("\nWhat trick shall Sparky do?");
System.out.println("Roll Over");
System.out.println("Jump");
System.out.println("Sit");
System.out.println("Bark");

System.out.print("\nYour command: ");
String command = keyboard.nextLine();

Yet in my dog class I wish to retrieve the inputted command in a method and perform the calculations in there, for instance:

public String getResponse()
{
    if (command.equalsIgnoreCase("Roll Over"))
    {
        // Roll Over Code
        response = "I just Rolled Over!";
    }
    // rest of the commands
    return response;
}

I believe an easy option is to make the variable 'command' public in the driver class and use:

if (dog.command.equalsIgnoreCase("Roll Over"))
// rest of code

But I hear it's not advisable to make your variables public. From what I've gathered I can return a variable's value to the driver class with 'return 'variable', but how can I return a variables value to a class (ie dog) from the driver class?

Why dont you change the signature of getResponse method in Dog class as below:

public String getResponse(final String command)
{
    if (command.equalsIgnoreCase("Roll Over"))
    {
        // Roll Over Code
        response = "I just Rolled Over!";
    }
    // rest of the commands
    return response;
}

As much as i understand, you are going to create and instance of Dog class in DogDriver class. You can call getResponse and pass user input command to it.

M confused what u asked what i understood is u are not able to return the response to the dog driver class so for that just call getResponse method from your dog driver class :

public class DogDriver{
public static void main(String args[]){
    System.out.println("\nWhat trick shall Sparky do?");
    System.out.println("Roll Over");
    System.out.println("Jump");
    System.out.println("Sit");
    System.out.println("Bark");

    System.out.print("\nYour command: ");
   final String command = keyboard.nextLine();

    Dog dog = new Dog();
   String response = dog.getResponse(command);
    System.out.println(response);
}
}

And your dog class code will be

public String getResponse(final String command)
{
    if (command.equalsIgnoreCase("Roll Over"))
    {
        // Roll Over Code
        response = "I just Rolled Over!";
    }
    // rest of the commands
    return response;
}

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