我在理解以下代码时遇到问题,如何将heavenlyBody.addSatellite(satellites );添加到HashMap中?

HeavenlyBody planet1 = solarSystem.get("Mars");被称为planet1.getSatellites()时,火星行星返回火卫一和Deimos卫星,植物地球返回卫星Moon。

我的问题是如何将Phobos和Deimos添加到solarSystem HashMap的火星钥匙中?

   public class Main {

    public static void main(String[] args) {

        Map<String, HeavenlyBody> solarSystem = new HashMap<>();

        // Create Planet
        HeavenlyBody heavenlyBody = new HeavenlyBody("Mars", 10.2);
        solarSystem.put(heavenlyBody.getName(), heavenlyBody);

        // Create Satellite
        HeavenlyBody satellites  = new HeavenlyBody("Phobos", 01.2);
        solarSystem.put(satellites .getName(), satellites );
        heavenlyBody.addSatellite(satellites );

        // Create Satellite
        satellites  = new HeavenlyBody("Deimos", 01.2);
        solarSystem.put(satellites .getName(), satellites );
        heavenlyBody.addSatellite(satellites );

        heavenlyBody = new HeavenlyBody("Earth", 365);
        solarSystem.put(heavenlyBody.getName(), heavenlyBody);

        satellites  = new HeavenlyBody("Moon", 27);
        solarSystem.put(satellites .getName(), satellites );
        heavenlyBody.addSatellite(satellites );

        // Get the List of Satellites for Planet Mars and Earth
        HeavenlyBody planet1 = solarSystem.get("Mars");
        System.out.println("Planet :" + planet1.getName());
        for (HeavenlyBody satellite : planet1.getSatellites()) {
            System.out.println(" \t" + satellite.getName());
        }

        HeavenlyBody plant2 = solarSystem.get("Earth");
        System.out.println("Planet :" + plant2.getName());
        for (HeavenlyBody satellite : plant2.getSatellites()) {
            System.out.println(" \t" + satellite.getName());
        }
    }
}


HeavenlyBody.java

public class HeavenlyBody {

    private String planetName;
    private double orbitalPeriod;

    private Set<HeavenlyBody> satellites;

    public HeavenlyBody(String planetName, double orbitalPeriod) {
        super();
        this.planetName = planetName;
        this.orbitalPeriod = orbitalPeriod;
        this.satellites = new HashSet<>();
    }

    public Set<HeavenlyBody> getSatellites() {
        return new HashSet<>(this.satellites);
    }

    public String getName() {
        return planetName;
    }

    public double getOrbitalPeriod() {
        return orbitalPeriod;
    }

    public void addSatellite(HeavenlyBody satellite) {
        this.satellites.add(satellite);
    }
}


输出:

Planet :Mars
    Phobos
    Deimos
Planet :Earth
    Moon


有人可以解释一下对象引用是如何分配给对象的,并带有图表,或者如何在HashMap中添加卫星?

最佳答案

solarSystem.put(heavenlyBody.getName(), heavenlyBody);中,将键Mars放入值为heavenlyBody的哈希映射中。因此,该值引用了与火星有关的对象。



HeavenlyBody satellites  = new HeavenlyBody("Phobos", 01.2);
solarSystem.put(satellites .getName(), satellites );
heavenlyBody.addSatellite(satellites );


首先创建卫星Phobos,然后将其放置在哈希表中,并以密钥Phobos和对象satellites作为对象。然后在heavenlyBody.addSatellite(satellites );中,变量heavenlyBody仍然是对火星的引用,它只是作为卫星添加到那里的。哈希图中的相应值引用相同的对象heavenlyBody。当哈希映射引用该对象时,它“看到” heavenlyBody中的更新。

10-01 19:55