简体   繁体   English

如何从所属类的实例变量之一中获取类的特定实例

[英]How to get the specific instance of a class from one of the instance variables that belong to it

Let's say I have multiple instances of this class: 假设我有该类的多个实例:

public class Star {
    public Star(ArrayList<Planet> planets, ArrayList<Comet> comets) {
        pla = planets;
        com = comets;
    }
    // Getters and setters go here

    private ArrayList<Planet> pla;
    private ArrayList<Comet> com;
}

How can I work with the instance of Star that, say, a specific Planet belongs to? 我如何处理某个特定Planet所属的Star实例? For example, see this pseudocode: 例如,请参见以下伪代码:

aSpecificPlanet.getInstanceOfClassThisBelongsTo().doStuffWithIt();

I've tried this in one of Planet 's getters: 我已经在Planet的一个吸气剂中尝试过此方法:

public Star getStar() {
    return this.getClass();
}

But I am getting this error: incompatible types: Class<CAP#1> cannot be converted to Star 但我收到此错误: incompatible types: Class<CAP#1> cannot be converted to Star

How can I do this? 我怎样才能做到这一点?

You're getting a Class instance and not a Star instance. 您得到的是Class实例,而不是Star实例。

You can add a field in Planet of type Star which would reference the Star to which it belongs. 您可以在类型为Star Planet中添加一个字段,该字段将引用它所属的Star Whenever you add a Planet to a Star , this field can be set: 每当将“ Planet添加到“ Star ,都可以设置以下字段:

Star star = new Star(planets, comets);
for(Planet planet : planets) {
   planet.setStar(star);  // add this field
}

You can place this inside a utility method to avoid duplicating code and to ensure consistency: 您可以将其放在实用程序方法中,以避免重复代码并确保一致性:

public void addPlanet(Star star, Planet planet) {
   if(star != null && planet != null) {
      star.getPlanets().add(planet); // assuming the list is always not null
      planet.setStar(star);
   }
}

Misread the question - so here's a better answer: 误解了问题-因此有一个更好的答案:

What you are looking for is the typical use case of an 1:n relation in relational databases - you have 1 star having n planets and each of the planets belong to 1 star. 您正在寻找的是关系数据库中1:n关系的典型用例-您有1颗恒星,其中n颗行星,每个行星都属于1颗恒星。 In the database you'd model this with a foreign key in each table. 在数据库中,您可以在每个表中使用外键对此建模。

In Java, you could map it the same way (which you actually do when trying to model this relation via JPA): The star contains a list of all planets and the planet has a field containing the star. 在Java中,可以用相同的方式进行映射(尝试通过JPA对这种关系进行建模时实际上要执行的操作):恒星包含所有行星的列表,并且行星具有包含恒星的字段。

You really cant'. 你真的不能。 As shown, there is no association from planet to star. 如图所示,行星与恒星之间没有关联。

You may wish to review your class hierarchy. 您可能希望查看您的类层次结构。 You current have Star with a 'has-a' relationship to Planet and Comet. 您当前拥有与Planet和Comet之间“拥有”关系的Star。 Isn't it really the galaxy that contains all 3? 真的是包含所有这三个星系的星系吗? Or maybe you want Planet to have 'has-a' relationship to Star 或者,也许您希望Planet与Star具有“亲密”关系

You need to have a reference to the star inside your planet. 您需要参考行星内部的恒星。 you can't get the instance without it. 没有它,您将无法获得实例。

public class Planet {
private Star star;

    public void setStar(Star star)
    {
       this.star = star;
    }
    public Star getStar()
    {
        return this.star;
    }
}

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

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