简体   繁体   English

Java从另一个类获取对象列表

[英]Java Get the List of an Object from another Class

I'm having two classes : WorldOfRobots and Robot (abstract). 我有两节课:WorldOfRobots和Robot(抽象)。 Both are public. 两者都是公开的。 World of robots is basically an arraylist of Robot. 机器人世界基本上是机器人的数组列表。 Then I have a class telebot which is an extension of Robot. 然后我有一个class telebot,它是Robot的扩展。 I'm trying to build a method in the class Telebot which will identify and get the list of robots where the current object Telebot is. 我正在尝试在Telebot类中构建一个方法,该方法将识别并获取当前对象Telebot所在的机器人列表。 For example: I create 2 World of Robots (wor1 and wor2) and then 1 telebot (r1). 例如:我创建2个机器人世界(wor1和wor2),然后创建1个远程机器人(r1)。 I add r1 in wor1. 我在wor1中添加r1。 I'd like to get a way to get the list of robots of wor1 in a method of the class telebot. 我想用Telebot类的方法来获得wor1机器人列表。

Here is a bit of code. 这是一些代码。

abstract class Robot {
// content
}

public class Telebot extends Robot
{
    // instance variables - replace the example below with your own
    private WorldOfRobots wor;

    public Telebot(String newName, String newDirection)
    {
        super(newName, newDirection);
    }

    public void something {

        // here I'm trying to get the list
        wor = new WorldOfRobots();
        ArrayList<Robot> robots = wor.getList();
        // Unfortunately this solution doesn't work cause I'm creating a new WOR. What I want is to get the WOR where the Telebot belong.
    }

}

public class WorldOfRobots {

// List of robots
private ArrayList<Robot> robots;

    public WorldOfRobots() {
    robots = new ArrayList<Robot>();
    }

    public ArrayList<Robot> getList() {
        return robots;
    }

}

Thanks for your help. 谢谢你的帮助。

You can refactor your class to something like this ... 您可以将您的班级重构为这样的东西...

public class Telebot extends Robot {

//your code and constructer here

public void something(WorldofRobots container){
  //this is the list containing your instance of telerobot, use it as you like
}

}

Now from outside classes you can invoke robotInstance.something(listOfRobot); 现在,您可以从外部类调用robotInstance.something(listOfRobot); I am not sure how your classes are exactly interacting so I cannot expand on using this method more. 我不确定您的班级之间是如何进行精确交互的,所以我无法进一步扩展使用此方法。

abstract class Robot {
private WorldOfRobots world;

public void setWorld(WorldOfRobots world)
{
    this.world=world;
}
// content
}

public class Telebot extends Robot
{
    public Telebot(String newName, String newDirection)
    {
        super(newName, newDirection);
    }   
    public void doSomething()
    {
        world.doSomethingElse();
    }

}

public class WorldOfRobots {

// List of robots
private ArrayList<Robot> robots;

    public WorldOfRobots() {
    robots = new ArrayList<Robot>();
    }
    public void addRobot(Robot robot)
    {
        robots.add(robot);
        robot.setWorld(this);
    }

}

Storing a reference for the WorldOfRobots in the Robot class is reasonable in this case. 在这种情况下,将WorldOfRobots的引用存储在Robot类中是合理的。 If you want a robot to belong to multiple WorldOfRobots then change the world varible to List. 如果您希望一个机器人属于多个WorldOfRobots则将世界变量更改为List。

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

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