我在寻找解决问题的方法时找到了这个网站,我想知道是否有人可以帮助我尝试解决我的困境。
我正在使用XNA和C#创建一些用户可以随机选择部件组装汽车的东西。汽车不必有4个轮子和挡风玻璃。它只需要由用户选择的至少7个部分组成。我创建了一个名为“Car”的类,它将创建用户选择的所有7个部件实例。这些部件属于“CarPart”类。我还创建了两个独立的“CarPart”子类,称为“Wheel”和“Windshield”。我打算稍后扩展这些部分,例如添加“Door”和“Trunk”等。
所以我所做的是在我的Car类中,我声明了7个类型为“CarPart”的对象,然后将它们声明为新的挡风玻璃()。或新轮()。
但是当我尝试访问Windshield或Wheel类的初始化函数时,它只允许我访问父“CarPart”类的初始化函数。
我是否尝试以正确的方式做到这一点,还是应该找到解决问题的另一种方法?
我也尝试过使用Virtual和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类并尝试使用挡风玻璃和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);
}
}
}
我会非常感激我得到的任何帮助! 提前谢谢!
答案 0 :(得分:1)
您的Windshield
和Wheel
都是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)
{
...
}
}
然后你可以写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
等等。