簡體   English   中英

無法在C#和XNA中正確初始化父類的子級

[英]Can't initialize child of parent class properly in C# and XNA

我在尋找問題的解決方案時找到了該網站,我想知道是否有人可以幫助我嘗試解決我的困境。

我正在使用XNA和C#創建一些東西,使用戶能夠從他們隨機選擇的零件組裝汽車。 該汽車不必具有4個車輪和一個擋風玻璃。 它只需要由用戶選擇的至少7個部分組成。 我創建了一個名為“ Car”的類,它將創建用戶選擇的零件的所有7個實例。 這些零件屬於“ CarPart”類。 我還創建了兩個單獨的“ CarPart”子類,分別稱為“車輪”和“風擋”。 我打算稍后再擴展這些部分,例如添加“ Door”和“ Trunk”等。

因此,我要做的是在Car類中,聲明7個類型為“ CarPart”的對象,然后將它們聲明為新的Windshield()。 或新的Wheel()。

但是,當我嘗試訪問Windshield或Wheel類的初始化函數時,它僅允許我訪問父級“ CarPart”類的初始化函數。

我是要嘗試以正確的方式執行此操作,還是應該找到其他解決此問題的方法?

我也嘗試過使用Virtual and Override嘗試重載功能,但是我似乎也無法使其正常工作,只是說:“找不到合適的方法來重載”。

我已經包括了我的代碼。

任何幫助都將是驚人的,我已經被困了好幾天了,如果沒有獲得用戶能夠按需組裝汽車的功能,我將無法繼續我的項目。

我在此鏈接中找到了與嘗試執行的操作類似的操作,但似乎無法正常工作。 http://csharp-station.com/Tutorial/CSharp/Lesson09

我確定我正在做一些我看不到的愚蠢的事情,因為我盯着同一問題已經太久了。

車類:

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類,並嘗試使用Windshield和Wheel類作為參數進行設置:

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);
        }
    }
}

我將非常感謝我獲得的任何幫助! 提前致謝!

您的WindshieldWheel都是CarPart后代。 對於Car類,它們只是CarPart (已定義),因此Car不知道它們中的任何一個是Wheel還是Windshield 至少不是在編譯時。

要解決此問題,您需要向這些類添加接口。 或在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)
   {
      ...
   }
}

然后,您可以在CarPartCarPart

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

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

因此,所有這些部分都符合IInitializable接口,您可以調用通用方法Initialize (Vector2) 當不同的不相關類實現相同的行為(例如IQueryableIDisposable等)時,接口最有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM