ASP.NET:不熟悉的接口 [英] ASP.NET: Unfamiliar with Interfaces

查看:112
本文介绍了ASP.NET:不熟悉的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建设ASP.NET/VB.NET与各种对象一个中型的应用程序......我从来没有使用的接口,当我提到这给他一个老乡程序员犹豫不决。谁能给我他们是如何使用的快速浏览,他们是用来做什么的,为什么我会使用它们?也许我并不需要使用他们为这个项目,但如果他们想帮忙,我肯定会喜欢尝试。

I'm building a decent sized application in ASP.NET/VB.NET with various objects... I've never used interfaces before, and a fellow programmer balked when I mentioned this to him. Can anyone give me a quick overview on how they're used, what they're used for, and why I would use them? Maybe I don't need to use them for this project, but if they would help, I surely would love to try.

太感谢了!

推荐答案

一旦你搞定的接口,OOP真的属于地方。简单地说,接口定义了一组公共方法签名,你创建一个实现了这些方法的类。这可以让你概括功能,从而实现了特定接口(具有相同的方法,即类)的任何类,即使那些类并不一定相互下降。

Once you "get" interfaces, OOP really falls into place. To put it simply, an interface defines a set of public method signatures, you create a class which implements those methods. This allows you generalize functions for any class which implements a particular interface (i.e. classes which have the same methods), even if those classes don't necessarily descend from one another.

Module Module1

    Interface ILifeform
        ReadOnly Property Name() As String
        Sub Speak()
        Sub Eat()
    End Interface

    Class Dog
        Implements ILifeform

        Public ReadOnly Property Name() As String Implements ILifeform.Name
            Get
                Return "Doggy!"
            End Get
        End Property

        Public Sub Speak() Implements ILifeform.Speak
            Console.WriteLine("Woof!")
        End Sub

        Public Sub Eat() Implements ILifeform.Eat
            Console.WriteLine("Yum, doggy biscuits!")
        End Sub
    End Class

    Class Ninja
        Implements ILifeform

        Public ReadOnly Property Name() As String Implements ILifeform.Name
            Get
                Return "Ninja!!"
            End Get
        End Property

        Public Sub Speak() Implements ILifeform.Speak
            Console.WriteLine("Ninjas are silent, deadly killers")
        End Sub

        Public Sub Eat() Implements ILifeform.Eat
            Console.WriteLine("Ninjas don't eat, they wail on guitars and kick ass")
        End Sub
    End Class

    Class Monkey
        Implements ILifeform


        Public ReadOnly Property Name() As String Implements ILifeform.Name
            Get
                Return "Monkey!!!"
            End Get
        End Property

        Public Sub Speak() Implements ILifeform.Speak
            Console.WriteLine("Ook ook")
        End Sub

        Public Sub Eat() Implements ILifeform.Eat
            Console.WriteLine("Bananas!")
        End Sub
    End Class


    Sub Main()
        Dim lifeforms As ILifeform() = New ILifeform() {New Dog(), New Ninja(), New Monkey()}
        For Each x As ILifeform In lifeforms
            HandleLifeform(x)
        Next

        Console.ReadKey(True)
    End Sub

    Sub HandleLifeform(ByVal x As ILifeform)
        Console.WriteLine("Handling lifeform '{0}'", x.Name)
        x.Speak()
        x.Eat()
        Console.WriteLine()
    End Sub
End Module

类的无上述相互下降,但我的HandleLifeform方法推广到对它们进行操作 - 或任何真正的类,它实现ILifeform接口

None of the classes above descend from one another, but my HandleLifeform method is generalized to operate on all of them -- or really any class which implements the ILifeform interface.

这篇关于ASP.NET:不熟悉的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆