可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
In C# I can create an interface, and when I use the interface the compiler knows that certain interface requirements are fulfilled by the base class. This is probably clearer with an example:
interface FormInterface { void Hide(); void Show(); void SetupForm(); } public partial class Form1 : Form, FormInterface { public Form1() { InitializeComponent(); } public void SetupForm() { } }
The compiler knows that Hide() and Show() are implemented in Form and the above code compiles just fine. I can't figure out how to do this in VB.NET. When I try:
Public Interface FormInterface Sub Hide() Sub Show() Sub SetupForm() End Interface Public Class Form1 Inherits System.Windows.Forms.Form Implements FormInterface Public Sub SetupForm() Implements FormInterface.SetupForm End Sub End Class
But the Compiler complains that Form1 must implement 'Sub Hide()' for interface 'FormInterface'. Do I actually have to add the following?
Public Sub Hide1() Implements FormInterface.Hide Hide() End Sub
On all my forms, or is a better route creating an abstract base class that has SetupForm() (and how do you do that in VB.NET)?
回答1:
No, System.Windows.Forms.Form
doesn't have the FormInterface implemented, so VB.NET doesn't know they match. VB.NET doesn't do implicit interface implementation, just explicit.
Yes, you should create a base class and implement your interface on that. I would, however, name them slightly different. Perhaps DoShow
and DoHide
.
Something like this:
Public Class BaseForm Inherits System.Windows.Forms.Form Implements FormInterface Public Sub SetupForm() Implements FormInterface.SetupForm End Sub Public Sub DoShow() Implements FormInterface.DoSHow Me.Show() End Sub Public Sub DoHide() Implements FormInterface.DoHide Me.Hide() End Sub End Class
Else you could by accident do this:
Public Class BaseForm Inherits System.Windows.Forms.Form Implements FormInterface Public Sub SetupForm() Implements FormInterface.SetupForm End Sub Public Sub Show() Implements FormInterface.SHow Me.Show() End Sub Public Sub Hide() Implements FormInterface.Hide Me.Hide() End Sub End Class
And that will crash and burn.
Don't make the baseclass MustInherit, because the forms designer won't like that.
回答2:
Unless you intended to use protected members of the Form
class in Form1
members, I would use a containment relationship instead of inheritance. So you would have your FormInterface
named something like IFormWrapper
instead, and your implementation would be like this (I show it in C# because it is my working language, I think you will be able to translate the idea to VB):
public class Form1 : IFormWrapper { private readonly Form form; public Form1 { this.form=new Form(); } public void Show() { form.Show(); } public void Hide() { form.Hide(); } public void SetupForm() { //Code for the setup method } }
After all, if you were planning to use Form1
instances through the FormInterface
class, you would have had no access to the Form
members other than Show
and Hide
.