强制派生class实现接口
Force derived class to implement interface
我今天(和昨天一样)在这里遇到另一个奇怪的界面问题。
我有一个 class:
public class InputDevice<T> where T : Button {
protected List<T> buttons = new List<T>();
protected InputDevice() {
//Only allow instanciation from within a derived class
}
}
如您所见,这个class无法实例化。
从它派生的 class 可能能够被实例化。
这是一个子class:
public sealed class Keyboard : InputDevice<KeyboardButton> {
public Keyboard() {
buttons.Add(new KeyboardButton(KeyCode.A));
}
}
到目前为止一切顺利。
现在我希望 InputDevice<T>
的所有派生者提供一个 GetButton()
方法。
该方法应将设备按钮的枚举类型作为参数。
对于 Keyboard
class 它看起来像这样:
public KeyBoardButton GetButton(KeyCode Code) {
//Retrieve button
}
对于 Mouse : InputDevice<MouseButton>
它看起来像:
public MouseButton GetButton(MouseButtons Button) {
//Retrieve button
}
注:MouseButton (class) != MouseButtons (enum)
InputDevice(T)
的每个派生程序都必须实现该方法。
但我不希望 InputDevice(T)
本身实现它,因为它不知道按钮的枚举类型 (f.e.KeyCode)。
它只知道按钮的类型,即T。
- 解决方法
将以下界面添加到InputDevice(T)
public interface IInputDevice{
void GetButton(System.Type);
}
- 问题:
InputDevice(T
) 必须实施它,但它不能。
我不知道 InputDevice(T)
的 return 类型 T
- 解法:
在所有派生程序中手动添加方法。
- 问题:
不保证提供者提供方法。
你有解决办法吗?在尝试解决这个问题时我真的很困惑。
只需声明InputDevice<T>
抽象
public abstract class InputDevice<T>
{
protected abstract void GetButton(System.Type type);
}
此外,您可以将 InputDevice<T>
设置为 IInputDevice
的继承者,这也可以。
您可以将基础 class 抽象化并更改其定义以包含密钥代码类型:
public abstract class InputDevice<TButton, TEnum> where TButton : Button {
public abstract TButton GetButton(TEnum Code);
}
然后你可以像这样定义派生的 classes:
public sealed class Keyboard : InputDevice<KeyboardButton, KeyCode> {
public override KeyboardButton GetButton(KeyCode Code) {
// implementation here...
}
}
我今天(和昨天一样)在这里遇到另一个奇怪的界面问题。
我有一个 class:
public class InputDevice<T> where T : Button {
protected List<T> buttons = new List<T>();
protected InputDevice() {
//Only allow instanciation from within a derived class
}
}
如您所见,这个class无法实例化。
从它派生的 class 可能能够被实例化。
这是一个子class:
public sealed class Keyboard : InputDevice<KeyboardButton> {
public Keyboard() {
buttons.Add(new KeyboardButton(KeyCode.A));
}
}
到目前为止一切顺利。
现在我希望 InputDevice<T>
的所有派生者提供一个 GetButton()
方法。
该方法应将设备按钮的枚举类型作为参数。
对于 Keyboard
class 它看起来像这样:
public KeyBoardButton GetButton(KeyCode Code) {
//Retrieve button
}
对于 Mouse : InputDevice<MouseButton>
它看起来像:
public MouseButton GetButton(MouseButtons Button) {
//Retrieve button
}
注:MouseButton (class) != MouseButtons (enum)
InputDevice(T)
的每个派生程序都必须实现该方法。
但我不希望 InputDevice(T)
本身实现它,因为它不知道按钮的枚举类型 (f.e.KeyCode)。
它只知道按钮的类型,即T。
- 解决方法
将以下界面添加到InputDevice(T)
public interface IInputDevice{
void GetButton(System.Type);
}
- 问题:
InputDevice(T
) 必须实施它,但它不能。
我不知道 InputDevice(T)
T
- 解法:
在所有派生程序中手动添加方法。
- 问题:
不保证提供者提供方法。
你有解决办法吗?在尝试解决这个问题时我真的很困惑。
只需声明InputDevice<T>
抽象
public abstract class InputDevice<T>
{
protected abstract void GetButton(System.Type type);
}
此外,您可以将 InputDevice<T>
设置为 IInputDevice
的继承者,这也可以。
您可以将基础 class 抽象化并更改其定义以包含密钥代码类型:
public abstract class InputDevice<TButton, TEnum> where TButton : Button {
public abstract TButton GetButton(TEnum Code);
}
然后你可以像这样定义派生的 classes:
public sealed class Keyboard : InputDevice<KeyboardButton, KeyCode> {
public override KeyboardButton GetButton(KeyCode Code) {
// implementation here...
}
}