Can';t在C#和XNA中正确初始化父类的子类

Can';t在C#和XNA中正确初始化父类的子类,c#,class,inheritance,parent,C#,Class,Inheritance,Parent,我在寻找解决我问题的方法时发现了这个网站,我想知道是否有人能帮助我尝试解决我的困境 我使用XNA和C#来创建一些东西,用户可以使用随机选择的零件组装汽车。这辆车不必有四个轮子和挡风玻璃。它只需要由用户选择的至少7个部分组成。我创建了一个名为“Car”的类,它将创建用户选择的所有7个零件实例。这些部分属于“CarPart”类。我还创建了“CarPart”的两个独立子类,称为“Wheel”和“挡风玻璃”。我打算在后面对这些部分进行扩展,例如添加“门”和“行李箱”等 所以我要做的是在我的Car类中,我

我在寻找解决我问题的方法时发现了这个网站,我想知道是否有人能帮助我尝试解决我的困境

我使用XNA和C#来创建一些东西,用户可以使用随机选择的零件组装汽车。这辆车不必有四个轮子和挡风玻璃。它只需要由用户选择的至少7个部分组成。我创建了一个名为“Car”的类,它将创建用户选择的所有7个零件实例。这些部分属于“CarPart”类。我还创建了“CarPart”的两个独立子类,称为“Wheel”和“挡风玻璃”。我打算在后面对这些部分进行扩展,例如添加“门”和“行李箱”等

所以我要做的是在我的Car类中,我声明了7个类型为“CarPart”的对象,然后将它们声明为new挡风玻璃()。或新车轮()

但当我尝试访问挡风玻璃或车轮类的初始化函数时,它只允许我访问父“CarPart”类的初始化函数

我是想用正确的方法来解决这个问题,还是应该用另一种方法来解决这个问题

我也尝试过使用Virtual和Override来尝试重载函数,但我似乎也不能让它正常工作,它只是说,“找不到合适的方法来重载”

我已经包含了我的类代码

任何帮助都将是惊人的,我已经被困了好几天,如果没有用户能够按照他们的意愿组装汽车的功能,我无法继续我的项目

我发现了一些类似于我在这个链接上尝试做的事情,但我似乎无法让它工作。

我肯定我在做一些我看不见的蠢事,因为我已经盯着同一个问题太久了

车辆等级:

namespace TestGame4
{
    class Car
    {
        //Car consists of 7 CarParts chosen by the user//
        //e.g.: 

        CarPart Part1, Part2, Part3, Part4, Part5, Part6, Part7; 

        public void Initialize(CarPart part1, CarPart part2, CarPart part3, CarPart part4, CarPart part5, CarPart part6, CarPart part7)
        {
            Part1 = part1;
            Part2 = part2;
            Part3 = part3;
            Part4 = part4;
            Part5 = part5;
            Part6 = part6;
            Part7 = part7;       

            ***//This is the part that doesn't work how I want it to. I want it to initialize 
            //as a Windshield class, but it's only initializing as a CarPart class//***
            part1.Initialize(

        }
    }
}

CarPart class:

namespace TestGame4
{
    public class CarPart
    {
        public void Initialize(string assetName, Vector2 Position)
        {

        }
    }
}
挡风玻璃等级:

namespace TestGame4
{
    public class Windshield : CarPart
    {
        public void Initialize(Vector2 Position)
        {

        }
    }
}
车轮等级:

namespace TestGame4
{
    public class Wheel : CarPart
    {
        public void Initialize(Vector2 Position)
        {

        }
    }
}
我的主要Game1类,其中我初始化了Car类,并尝试使用挡风玻璃和车轮类作为参数进行设置:

namespace TestGame4
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Car myCar;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            myCar = new Car();
            myCar.Initialize(new Windshield(), new Wheel(), new Windshield(), new Wheel(), new Windshield(), new Wheel(), new Windshield());           

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}
namespace TestGame4
{
/// 
///这是游戏的主要类型
/// 
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
我的车;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
/// 
///允许游戏在开始运行之前执行任何需要的初始化。
///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务
///相关内容。调用base.Initialize将枚举所有组件
///并对它们进行初始化。
/// 
受保护的覆盖无效初始化()
{
//TODO:在此处添加初始化逻辑
我的车=新车();
myCar.初始化(新挡风玻璃(),新车轮(),新挡风玻璃(),新车轮(),新挡风玻璃(),新车轮(),新挡风玻璃());
base.Initialize();
}
/// 
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
/// 
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
//TODO:使用此.Content在此处加载游戏内容
}
/// 
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///所有内容。
/// 
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
/// 
///允许游戏运行逻辑,例如更新世界,
///检查碰撞、收集输入和播放音频。
/// 
///提供计时值的快照。
受保护覆盖无效更新(游戏时间游戏时间)
{
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
//TODO:在此处添加更新逻辑
更新(游戏时间);
}
/// 
///这就是所谓的比赛应该平局的时候。
/// 
///提供计时值的快照。
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
//TODO:在此处添加图形代码
基础。抽签(游戏时间);
}
}
}
如果我得到任何帮助,我将不胜感激!
提前谢谢

您的
挡风玻璃
车轮
都是
CarPart
的后代。对于
Car
类,它们只是
CarPart
(定义),因此
Car
不知道它们中是否有车轮
挡风玻璃
。至少在编译时不是这样

要解决此问题,需要向这些类添加接口。或者在
CarPart
中添加一个更简单的抽象函数

abstract class CarPart
{
   public abstract void Initialize(Vector2 position);
   public void Initialize(string assetName, Vector2 position)
   {
   }
}
之后,两个子类都需要重写这个函数,否则它们也将是抽象的(抽象类不能用于创建对象)

接口非常简单:接口是您要求类执行的行为。比如说

interface IInitializable
{
   void Initialize (Vector2 position);
}
class WindShield: IIinitializable
{
   void Initialize (Vector2 position)
   {
      ...
   }
}
然后您可以在
CarPart
中编写:

IInitializable [] parts = new IInitializable [7];
parts[0] = new WindShield();
...
parts[6] = new Wheel();

parts[3].Initialize(new Vector2(0, 0));

因此,所有这些部分都符合接口
IInitializable
,您可以调用公共方法
Initialize(Vector2)
。当不同的无关类实现相同的行为时,接口是最有用的,比如
IQueryable
IDisposable
等等

非常感谢。这似乎解决了我的问题!但是如果我再次遇到问题,我会再问一次。你能解释一下吗