c# - 不保存自己信息的对象

时间:2018-02-18 13:00:13

标签: c# oop object

我有太阳系中所有行星的物体:

Forall models M in my project:
    Forall keys k used in M:
        If k is a descendant of a ManyToMany or Foreign or  ... key:
            If k points to ModelA:
                Forall instances x of M:
                    If x.k=a1:
                        x.k=y

然后我通过运行此函数为每个对象设置值:

public static Planet Mercury = new Planet("Mercury");
public static Planet Venus = new Planet("Venus");
public static Planet Earth = new Planet("Earth");
public static Planet Mars = new Planet("Mars");
public static Planet Jupiter = new Planet("Jupiter");
public static Planet Saturn = new Planet("Saturn");
public static Planet Uranus = new Planet("Uranus");
public static Planet Neptune = new Planet("Neptune");
public static Planet Pluto = new Planet("Pluto");

当我运行程序时,我将其设置为依次输出每个对象的所有值。但是,它只输出每个对象的最后一个对象(pluto)的值,尽管它们是单独打印的。

private static void SetPlanetInformation(Planet planet)
{
    Boolean MassDone = false;
    Boolean DistanceDone = false;

    Double Mass, Distance;

    do
    {
        Console.WriteLine("Please enter the mass of {0} in kg", planet.GetPlanetName());
        MassDone = Double.TryParse(Console.ReadLine(), out Mass);
        planet.SetPlanetMass(Mass);

        Console.WriteLine("Please enter the distance of {0} from the sun in m", planet.GetPlanetName());
        DistanceDone = Double.TryParse(Console.ReadLine(), out Distance);
        planet.SetPlanetDistance(Distance);

    } while (MassDone == false || DistanceDone == false);
}

请有人帮助我理解为什么每个星球都没有保留所提供的信息,而不是仅从每颗行星的最后一颗行星获取所有信息。

由于

编辑:

班级星球是:     公共阶层星球     {         私有静态字符串PlanetName;         私人静态双PlanetMass;         private static double PlanetDistance;

Console.WriteLine("Mass of {0} is {1} kg, and the distance from the sun is {2} m.", Mercury.GetPlanetName(), Mercury.GetPlanetMass(), Mercury.GetPlanetDistance());
Console.WriteLine("Mass of {0} is {1} kg, and the distance from the sun is {2} m.", Venus.GetPlanetName(), Venus.GetPlanetMass(), Venus.GetPlanetDistance());
Console.WriteLine("Mass of {0} is {1} kg, and the distance from the sun is {2} m.", Earth.GetPlanetName(), Earth.GetPlanetMass(), Earth.GetPlanetDistance());
Console.WriteLine("Mass of {0} is {1} kg, and the distance from the sun is {2} m.", Mars.GetPlanetName(), Mars.GetPlanetMass(), Mars.GetPlanetDistance());
Console.WriteLine("Mass of {0} is {1} kg, and the distance from the sun is {2} m.", Jupiter.GetPlanetName(), Jupiter.GetPlanetMass(), Jupiter.GetPlanetDistance());
Console.WriteLine("Mass of {0} is {1} kg, and the distance from the sun is {2} m.", Saturn.GetPlanetName(), Saturn.GetPlanetMass(), Saturn.GetPlanetDistance());
Console.WriteLine("Mass of {0} is {1} kg, and the distance from the sun is {2} m.", Uranus.GetPlanetName(), Uranus.GetPlanetMass(), Uranus.GetPlanetDistance());
Console.WriteLine("Mass of {0} is {1} kg, and the distance from the sun is {2} m.", Neptune.GetPlanetName(), Neptune.GetPlanetMass(), Neptune.GetPlanetDistance());
Console.WriteLine("Mass of {0} is {1} kg, and the distance from the sun is {2} m.", Pluto.GetPlanetName(), Pluto.GetPlanetMass(), Pluto.GetPlanetDistance());

然而,有人指出PlanetName,PlanetMass和PlanetDistance不应该是静态的,所以我现在已经改变了它。

1 个答案:

答案 0 :(得分:1)

public class Planet
{
    private string PlanetName;
    private double PlanetMass;
    private double PlanetDistance;

    public Planet(string name)
    {
        PlanetName = name;
    }

    public void SetPlanetName(string name)
    {
        PlanetName = name;
    }

    public string GetPlanetName()
    {
        return PlanetName;
    }

    public void SetPlanetMass(double mass)
    {
        PlanetMass = mass;
    }

    public double GetPlanetMass()
    {
        return PlanetMass;
    }

    public void SetPlanetDistance(double distance)
    {
        PlanetDistance = distance;
    }

    public double GetPlanetDistance()
    {
        return PlanetDistance;
    }
}

PlanetName,PlanetMass和PlanetDistance不应该是静态的。