我正在尝试与DX类接口并出现构造错误。
public interface IDevice
{
}
class vJoystick : Joystick, IDevice
{ //ERROR Joystick does not take 0 parms...
public vJoystick( Joystick joystick) {}
}
class vMouse : Mouse, IDevice
{ //ERROR Mouse does not take 0 parms...
public vMouse( Mouse mouse ) { }
}
public class DeviceFactory
{
public static IDevice Create( object device, DirectInput di, Guid g )
{
if ( device is Joystick ) return new vJoystick( new Joystick(di, g) );
if ( device is Mouse ) return new vMouse( new Mouse ( di ));
return new vJoystick( new Joystick( di, g ) );//defult
}
}
见上面的评论。如何让我的类传递我的类构造的参数?
这也是一个很好的用法,看起来像是过度杀人?
Joystick device = ( Joystick ) DeviceFactory.Create( ( Joystick ) new Object(), directInput, deviceGuid );
更新:根据以下建议添加此内容。
class vJoystick : IDevice, Joystick
{
private Joystick joystick;
public vJoystick( DirectInput di, Guid g )
{
joystick = new Joystick(di,g);
}
}
class vMouse : IDevice, Mouse
{
private Mouse mouse;
public vMouse( DirectInput di)
{
mouse = new Mouse( di);
}
}
答案 0 :(得分:1)
如果你真的需要继承(我不太相信是这种情况),试试这个:
public interface IDevice
{
}
class vJoystick : Joystick, IDevice
{
public vJoystick(DirectInput di, Guid g) : base(di, g) {}
}
class vMouse : Mouse, IDevice
{
public vMouse( DirectInput di ) : base(di) { }
}
public class DeviceFactory
{
public static IDevice Create( object device, DirectInput di, Guid g )
{
if ( device is Joystick ) return new vJoystick(di, g);
if ( device is Mouse ) return new vMouse(di);
return new vJoystick(di, g);//defult
}
}
基本上,由于您从vJoystick
派生Joystick
,base(di, g)
实际上与new vJoystick(di, g)
做同样的事情。您无法从C#中的基类型创建派生类型的实例(即扩展它)。
另外,如果你想使用.NET中的DX,我建议使用一个适用于.NET的托管包装器,例如SlimDX或SharpDX。你会节省很多时间,除非你非常了解你正在做什么,否则它也会显着改善你的表现。更不用说减少内存泄漏的发生率和其他有趣的东西:)