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