简体   繁体   English

如何在VB.NET中使用隐式实现创建接口

[英]How can I create an interface in VB.NET with implicit implementations

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. 在C#中,我可以创建一个接口,当我使用该接口时,编译器知道基类满足某些接口要求。 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. 编译器知道Hide()和Show()是在Form中实现的,上面的代码编译得很好。 I can't figure out how to do this in VB.NET. 我无法弄清楚如何在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'. 但编译器抱怨Form1必须为接口'FormInterface'实现'Sub Hide()'。 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)? 在我的所有表单上,或者是创建具有SetupForm()的抽象基类的更好的路径(以及如何在VB.NET中执行此操作)?

No, System.Windows.Forms.Form doesn't have the FormInterface implemented, so VB.NET doesn't know they match. 不, System.Windows.Forms.Form没有实现FormInterface,因此VB.NET不知道它们匹配。 VB.NET doesn't do implicit interface implementation, just explicit. VB.NET不做隐式接口实现,只是显式。

Yes, you should create a base class and implement your interface on that. 是的,您应该创建一个基类并在其上实现您的接口。 I would, however, name them slightly different. 但是,我会将它们命名为略有不同。 Perhaps DoShow and DoHide . 也许是DoShowDoHide

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. 不要创建基类MustInherit,因为表单设计者不会喜欢它。

Unless you intended to use protected members of the Form class in Form1 members, I would use a containment relationship instead of inheritance. 除非您打算在Form1成员中使用Form类的受保护成员,否则我将使用包含关系而不是继承。 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): 所以你可以让你的FormInterface命名为IFormWrapper ,你的实现将是这样的(我在C#中展示它,因为它是我的工作语言,我认为你将能够将这个想法翻译成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 . 毕竟,如果您计划通过FormInterface类使用Form1实例,那么除了ShowHide之外,您将无权访问Form成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM