简体   繁体   中英

In Java can I reference an object made in one class in another?

I have 3 classes:

  • Main
  • Commands
  • Team

Inside Main I am creating an object which uses a method inside Team :

Team humanTeam = new Team("Humans");
Team monsterTeam = new Team("Monsters");

Is it possible to include that inside Commands without recreating an object? or is it okay to recreate it?

Example of humanTeam being called inside Commands:

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        Player p = (Player)sender;
                if(label.equalsIgnoreCase("joinhuman")){
                Team.addPlayer(humanTeam, p); //This line.
                p.sendMessage("You just joined the human team!");
            }

Thanks

At a quick glance one might suggest to make addPlayer a static method, but that might not be an ideal design in this scenario. Because Team isn't a static concept, but rather an instance concept:

Team humanTeam = new Team("Humans");
Team monsterTeam = new Team("Monsters");

You have two specific instances of Team , as opposed to one generic concept of Team . So you're going to want to add a player to an instance of Team rather than to the concept of Team . It's really just semantics, but I think makes for a cleaner implementation of separating instance concepts from static concepts.

So what you're likely looking to do (in what appears to be a Minecraft Bukkit plugin) is track your Team instances in some meaningful way so that various events and objects in your plugin can access those stored instances. A database is probably best for this, particularly if you want to persist the concept of teams between game re-starts. If that's not a requirement, then you could take your instances of Team s and store them statically. Given the hard-coded lines above, it seems reasonable that you could just as daily hard-code them on the Team class:

class Team {
    private static humanTeam;
    private static monsterTeam;

    // initialize them inline or in the static initializer

    public static Team getHumanTeam {
        return humanTeam;
    }

    public static Team getMonsterTeam {
        return monsterTeam;
    }

    // etc.
}

You can refer to these "get" methods in this case as "factory methods" for your teams. If you were to add more kinds of teams over time then you might make a more general "factory" which accepts some kind of team identifier (a string for the team's name for example). Those teams might also be stored as a dictionary of team names and instances. Ultimately what you're doing in this setup is keeping the management of "teams" encapsulated to the Team class. As the overall logic grows, you might separate the concerns even further by extracting a TeamMeneger class just to hold the logic of managing teams.

Now any class in your game can statically reference those specific instances:

Team.getHumanTeam().addPlayer(p);

(Another important benefit of this is that you can mock your factory methods in unit tests, and you'll find in general that instance logic is a lot easier to unit test than static logic.)

If your Commands have dependency on Team class you can do something like this.

    public class Commands{
    private Team humanTeam;
    private Team monsterTeam;

    public Team getHumanTeam() {
        return humanTeam;
    }

    public void setHumanTeam(Team humanTeam) {
        this.humanTeam = humanTeam;
    }

    public Team getMonsterTeam() {
        return monsterTeam;
    }

    public void setMonsterTeam(Team monsterTeam) {
        this.monsterTeam = monsterTeam;
    }
 }

Now when you create Team object like this

Team humanTeam = new Team("Humans");
Team monsterTeam = new Team("Monsters");

just set the Objects to the Commands class properties like this

Commands commands = new Commands();
commands.setHumanTeam(humanTeam);
commands.setMonsterTeam (monsterTeam );

Now you can use them to manipulate the Commands class.

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