简体   繁体   中英

Referencing created objects with helper classes

Happy January everyone,

Recently I've been learning Java from scratch. It's been really fun. I've started to create a basic RPG in which the user selects and attributes and whether the user wants to be a warrior or a mage. Good times.

In my main class I have this new hero object now that has stored the users inputs for their desired attributes and whether or not they want to be a warrior or mage. Now I'd like to create other classes to REFERENCE that newly created object with stored variables. I'm completely lost.

Since I don't want to paste in an embarrassing amount of crappy code I'll just use a really simple example:

Here is the first class where the user tells us how much strength his hero has:

import java.util.Scanner;

public class HeroCreation
{
    int strength;
    Scanner input = new Scanner(System.in);

    public void setStr()
    {
        System.out.println("Please tell stackoverflow how much strength you want: ");
        strength = input.nextInt();
        System.out.println("Your strength is: " + strength + "!");
    }

}

Here is the main class that runs this method:

public class MainClass
{
    public static void main(String args[])
    {
        HeroCreation hero1 = new HeroCreation();
        hero1.setStr();

        //Here is where I want to reference another class that refers to this new hero...

    }
}

Here's where I'm stuck. I have this new hero. This hero has 10 strength. How do I refer to said hero in other helper classes? Conceptually, am I thinking about this wrong?

Thank you for your time and expertise!

One option would be to persist the data somewhere, be it in a database or a file.

Or create an instance of the second class you want to use and simply pass hero1 to the relevant method.

Often when we talk about helper classes we refer to classes that use static factory methods such that they do not have to be initialized.

Bro, you don't even have a Hero yet, all you have is a HeroCreation .

A couple basic object-oriented analysis and design concepts for you:

  • As a starting design rule, you should be creating a class, or at least a variable, for every noun in your problem space, and you should be creating a method for every verb in your problem space.

  • When writing new code, the first thing you should think is "what class is responsible for this"? Delegation of responsibility to the correct class is very important to keep your code easy to understand and expand. Often a new responsibility does not belong to any existing class, so you have to add a new class.

Utilizing those two rules, here's a starting version of how I would write the code you have so far.

Hero.java

public class Hero
{
    private int strength;
    private int intelligence;
    // other attributes

    public int getStrength() {
        return this.strength;
    }

    public void setStrength(int strength) {
        this.strength = strength;
    }

    public int getIntelligence() {
        return this.intelligence;
    }

    public void setIntelligence(int intelligence) {
        this.intelligence = intelligence;
    }

    // other "accessor" (get and set) methods for attributes
}

HeroCreation.java

import java.util.Scanner;

public class HeroCreation
{
    Scanner input = new Scanner(System.in);

    public int askStrength() {
        return askIntegerAttribute("strength");
    }

    public int askIntelligence() {
        return askIntegerAttribute("intelligence");
    }

    // ask other attribute values

    private int askIntegerAttribute(String attribute) {
        System.out.println("How much " + attribute + " do you want? ");
        int value = input.nextInt();
        System.out.println("Your " + attribute + " is: " + value + "!");
        return value;
    }
}

Main.java

public class Main
{
    public static void main(String args[])
    {
        HeroCreation creation = new HeroCreation();

        Hero hero1 = new Hero();
        hero1.setStrength(creation.askStrength());
        hero1.setIntelligence(creation.askIntelligence());

        Hero hero2 = new Hero();
        hero2.setStrength(creation.askStrength());
        hero2.setIntelligence(creation.askIntelligence());

    }
}

Now you would continue adding variables and methods to these classes, that fit within their defined responsibilities. And you would continue creating other classes for other responsibilities you encounter.

Based off your question and your comments, I think you mean something like this:

public static class MyHelperClass {

    public static void DoSomethingWithHeroStrength(HeroCreation hero)
    {
        int strength = hero.getStrength();
        // ...
    }
}

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