用C#中的接口继承windows窗体
我有一个基本的 Windows 窗体类,其中包含两个按钮:“确定”和“取消”。这个基本表单实现了接口IBaseForm
:
public interface IBaseForm
{
string FormText {get;set;}
DialogResult ShowView(IWin32Window owner);
}
public partial class BaseForm : IBaseForm
{
...
}
接下来我有一个窗口类,它继承自BaseForm
并实现了另一个接口IItem
:
public interface IItem : IBaseForm // here is problem
{
string ItemName {get; set;}
...
}
public partial class AddItemForm : BaseForm, IItem
{
...
// I don't have to implement IItem here, because it is implement in BaseForm
}
问题在于IBaseForm
的继承;我必须继承它两次。 在我的代码中:
IItem view = new AddItemForm();
view.FormText = "Add new item";
如果我从 IItem
中删除继承,view.FormText
将不可见。如果我从 BaseForm
中删除继承,我必须在每个 AddItemForm
中实现 IBaseForm
。我只展示了一个 itemForm,但它很多,而且我必须多次实现。
我该如何解决这个问题?
I have a base windows forms class which contains two buttons: Ok and Cancel. This base form implements the interface IBaseForm
:
public interface IBaseForm
{
string FormText {get;set;}
DialogResult ShowView(IWin32Window owner);
}
public partial class BaseForm : IBaseForm
{
...
}
Next I have a window class which inherits from BaseForm
and implements another interface IItem
:
public interface IItem : IBaseForm // here is problem
{
string ItemName {get; set;}
...
}
public partial class AddItemForm : BaseForm, IItem
{
...
// I don't have to implement IItem here, because it is implement in BaseForm
}
The problem is with the inheritance of IBaseForm
; I have to inherit it two times.
In code I have:
IItem view = new AddItemForm();
view.FormText = "Add new item";
If I remove inheritance from IItem
, view.FormText
will be not visible. If I remove the inheritance from BaseForm
I have to implement IBaseForm
in each AddItemForm
. I've only showed one itemForm but it is a lot, and I have to implement this many times.
How I can resolve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有其他方法可以做到这一点。 BaseForm 是一个类,而 IItem 是一个不相关的接口。两者都不是另一个的“超级”或“基础”,因此您需要从它们中的 IBaseForm 继承。
There isn't another way to do this. BaseForm is a class, whereas IItem is an unrelated interface. Neither is a 'super' or 'base' of the other, so you need to inherit from IBaseForm in both of them.
我不太明白这个问题;您编写的代码可以像您编写的那样编译并运行得很好。
AddItemForm
继承了BaseForm
对IBaseForm
的实现,因此不必重新实现IBaseForm
。I don't really understand the problem; the code you've written compiles and runs just fine as you've written it.
AddItemForm
inheritsBaseForm
's implementation ofIBaseForm
, so doesn't have to re-implementIBaseForm
.