嘿,我在想,如果我可以自己做一个班级的例子......
我的问题是我为行星及其卫星创建3D球体,其数据保存在对象中.我将参数传递给我的行星类的构造函数为"大小""轨道半径""纹理""革命速度"etcetra.我必须为Moon的行星制作另一个课程,它与月球课程完全相同.
我在想是否可以在自己内部创建类对象.传递一个参数列表\对象的数组自己创建和喜欢为地球我将通过"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
}
Run Code Online (Sandbox Code Playgroud)
有可能吗?
或者这种问题的最佳实践方法是什么?
ps我正在使用C#和Microsoft XNA Framework
是的,为什么不?但是你可能想要创建一个基类类型CelestialBody,你的类Planet和Moon类都将在其中.而且你不必将Planets 传递给Moon构造函数,但你可以Planet看起来像这样:
public class Moon : CelestialBody
{
//Moon-only properties here.
}
public class Planet : CelestialBody
{
//Planet-only properties here.
public List<Moon> Moons { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后添加Moon如下:
myPlanet.Moons.Add(new Moon(...));
Run Code Online (Sandbox Code Playgroud)
例如,抽象一些信息,因为a Moon不是Planet.