简体   繁体   English

在Java中,我可以引用在另一个类中的一个类中创建的对象吗?

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

I have 3 classes: 我有3节课:

  • Main 主要
  • Commands 命令
  • Team 球队

Inside Main I am creating an object which uses a method inside Team : Main内部,我正在创建一个使用Team内部方法的对象:

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

Is it possible to include that inside Commands without recreating an object? 是否可以在不重新创建对象的情况下将其包含在Commands中 or is it okay to recreate it? 还是可以重新创建它?

Example of humanTeam being called inside Commands: 在命令内部调用humanTeam的示例:

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. 乍一看,可能建议将addPlayer静态方法,但这在这种情况下可能不是理想的设计。 Because Team isn't a static concept, but rather an instance concept: 因为Team不是一个静态概念,而是一个实例概念:

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 . 你有两个具体事例 Team ,而不是一个通用概念 Team So you're going to want to add a player to an instance of Team rather than to the concept of Team . 所以你会希望玩家加入的实例 Team ,而不是概念 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. 因此,您可能想做的事情(似乎是Minecraft Bukkit插件)是以某种有意义的方式跟踪您的Team实例,以便插件中的各种事件和对象都可以访问这些存储的实例。 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. 如果这不是必需的,那么您可以使用Team的实例并将其静态存储。 Given the hard-coded lines above, it seems reasonable that you could just as daily hard-code them on the Team class: 鉴于上面的硬编码行,您似乎可以每天像在Team类上硬编码它们一样合理:

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. 最终,您在此设置中要做的就是将“团队”的管理封装到Team类中。 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. 随着整体逻辑的发展,您可以通过提取TeamMeneger类以保持团队管理的逻辑来进一步分离问题。

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. 如果您的命令依赖于Team类,则可以执行以下操作。

    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 commands = new Commands();
commands.setHumanTeam(humanTeam);
commands.setMonsterTeam (monsterTeam );

Now you can use them to manipulate the Commands class. 现在,您可以使用它们来操作Commands类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:为什么我不能在一个语句中声明引用变量并在类的另一个语句中创建引用的对象? - Java: Why can't I declare the reference-variable in one statement and create the referenced object in another statement of the class? 当不同的类加载器提供一个类的多个版本时,如何引用一个特定的版本? - When multiple versions of a class are made available by different class loaders, how can I reference a particular one? Java:我应该如何从另一个类引用公共对象? - Java: How should I reference a public object from another class? Java中的一个类可以控制另一类的对象吗? - Can one class in java control object in another class? 如何引用或访问Java Swing中用其他方法创建的对象(例如按钮)? - How can I reference or access objects such as buttons made in another method in Java Swing? 如何将引用从一个对象移动到另一个对象? - How can I move a reference from one object to another? 如何在Java中将在一个类中创建的对象传递给另一个类? - How do I pass an object created in one class to another in java? 我如何将请求对象从一个类传递到另一个类 - how can i pass request object from one class to another 我如何在另一个类中使用一个文件对象 - how can i use one file object in another class 如何在Java中将变量从一类传递到另一类? - How can I pass variables from one class to another in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM