vb.net 接口(interface)


用来实现不同类中,用统一方法来调用类中的各自方法和属性。

可以通过CTYPE(类名,接口名).接口方法来访问接口中的方法。

实例说明:

一个模块文件中写入

Public Interface 接口1
   ReadOnly Property id() As Long '定义一个只读的属性
  Function onll(byval en As String ) As Boolean '定义一个过程
End Interface 
Class BaseClass1: Implements 接口1
   Private m_id As Long=3 
   Public readonly Property idi As Long Implements 接口1.id 
      Get
         Return m_id
      End Get
   End Property
  Public  Function onll(en As String ) As Boolean Implements 接口1.onll 
     If en="接口" Then
          m_id+=1
      Return True     
      Else
         Return False 
      End If
   End Function
End Class

Class BaseClass2: Implements 接口1
   Private m_id As Long=3
   Public readonly Property idi As Long Implements 接口1.id 
      Get
         Return m_id
      End Get
   End Property
  Public  Function onl(en As String ) As Boolean Implements 接口1.onll 
     If en="接口x" Then
        m_id+=1
      Return True     
      Else
         Return False 
      End If
   End Function
End Class

 

Public   Sub showis_id(p As 接口1)'定义接口实现方法
      p.onll("接口") '传递给各自类中的方法
      MsgBox (p.id.ToString )'显示各自类中的ID.
End sub

 

在过程中加入:

      Dim ss As BaseClass1=New BaseClass1 
      Dim s1 As BaseClass2 =New BaseClass2 
      showis_id(s1)
      showis_id(ss)

类就会实现各自类中的方法,不必要考虑类中的哪个方法在实现。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM