2011-05-12 17 views
7

嘿,我在想,如果我可以做一个类的实例,在本身...对象的类的

我的问题是,我是个创造三维球的行星&它们的卫星,其数据我保持在对象中。我将参数传递给我的星球类的构造函数中的“大小”“轨道半径”“纹理”“革命速度”etcetra。我必须为月球的行星制作另一个班级,这个班级与月球班完全相同。

我在想如果我可以在它自己内部的类对象。传递一个参数给自己的对象的列表\数组,创建并像地球一样,我会传递“1”来创建一个月亮,并且因为月亮将具有相同的构造函数,所以我会在没有月亮的月亮时传递“0”。创造。

像这样的事情

class Planet 
{ 
    Model  u_sphere; 
    Texture2D u_texture; 
    //Other data members 

    List<Planet> Moons = new List<Planet>(); 

    Planet() 
    { 
    //Default Constructor 
    } 

    //Overloaded\Custom Constructor 
    Planet(Model m, Texture2D t, int moon_count) 
    { 
     u_sphere = m; 
     u_texture = t; 

     while(moon_count > 0) 
     { 
      Model  moon_sphere = LoadMesh("moon.x"); 
      Texture2D u_texture = LoadTexture("moon.bmp"); 
      Planet temp  = new Planet(moon_sphere,moon_texture,0); 
      Moons.Add(temp); 
      moon_count--; 
     } 
    } 
    //Others Getters & Setters 
} 
  • 是否有一些如何可能?

  • 或什么是这种问题的最佳做法\方法?

P.S我使用C#&微软X.N.A框架

+0

为什么不试试呢?这就像你写的一样。 (当然,'while'循环可以变得更简单。) – 2011-05-12 13:09:33

回答

7

是的,为什么不呢?但是您可能想要创建一个类型为CelestialBody的基类,其中您的PlanetMoon类别都将被盗取。你不必通过一个PlanetMoon s转换构造函数,但你可以只是让Planet这个样子的:

public class Moon : CelestialBody 
{ 
    //Moon-only properties here. 
} 

public class Planet : CelestialBody 
{ 
    //Planet-only properties here. 
    public List<Moon> Moons { get; set; } 
} 

然后添加Moon就像这样:

myPlanet.Moons.Add(new Moon(...)); 

例如抽象的一些信息,因为Moon不是Planet

+0

乔希的方法完成了我想说的同样的事情。这两个类将具有共同的属性。我不确定我是否会使用接口 - 我会走行星和卫星继承的基类CelestrialBody的路线。然后,Planet将拥有自己的附加属性(列表,Aphelion,Perihelion),卫星将拥有自己的属性(近地点,远地点)。 – 2011-05-12 13:31:34

+0

我最初也是将它作为基类的,这可能是我实现它的原因。 – 2011-05-12 13:33:57

+0

+1代替基准班:) – 2011-05-12 13:38:42

0

肯定。你只是再次使用这个类。请记住,该类的某些属性可能不再适用(有没有月亮的卫星,有吗?)

您可能需要添加一个构造函数来传递布尔值isChild。这样嵌套的孩子可以意识到它确实是一个孩子。

+2

月亮实际上可以有月亮。 – asawyer 2011-05-12 13:17:53

0

你已经看起来相当准确。一种可能的改进是创建一个名为Moon的新类并从Planet继承。这样,您可以为Moon添加其他属性/功能,例如存储对拥有Planet的引用。

0

当然,这是有效的代码。

虽然这种类型的设计可能有其他原因可疑。为什么Planet类会知道如何创建其他Planet实例等等。如果创建行星的逻辑超出了类imo,那么它会更清晰。

0

虽然你的代码是好的,但它看起来就像我的口味太接近一个小环。如果你曾经将0改为1,那么BANG!一种更健壮的方法是创建一个额外的构造函数并链接它们。这样就没有递归。

Planet(Model m, Texture2D t, int moon_count) : this(m, t) 
{ 
    while(moon_count > 0) 
    { 
     Model  moon_sphere = LoadMesh("moon.x"); 
     Texture2D u_texture = LoadTexture("moon.bmp"); 
     Planet temp  = new Planet(moon_sphere, moon_texture); 
     Moons.Add(temp); 
     moon_count--; 
    } 
} 

Planet(Model m, Texture2D t) 
{ 
    u_sphere = m; 
    u_texture = t; 
} 
2

更多面向对象的方法可以将任何月亮特定的代码分离到它自己的类中。这可能有助于让代码更加有组织,因为它变得更大。我知道月球不是真的一个星球,但谁在乎?

一个缺点然而这就是你现在限制你iheritance的选择,所以你就必须思考一个设计决策。

class Planet 
{  
    Model  u_sphere; 
    Texture2D u_texture; 
    List<Planet> Moons = new List<Planet>(); 

    Planet(){} 

    Planet(Model m, Texture2D t, int moon_count)  
    { 
     u_sphere = m; 
     u_texture = t; 

     while(moon_count > 0)   
     { 
      Planet temp  = new Moon(); 
      Moons.Add(temp); 
      moon_count--; 
     }  
    } 
} 


class Moon : Planet 
{ 
    Moon() 
    { 
     u_sphere = LoadMesh("moon.x"); 
     u_texture = LoadTexture("moon.bmp"); 
    } 
} 
相关问题