【发布时间】:2018-07-07 15:15:40
【问题描述】:
我有 2 个接口,IAccount 和 IEmailAccount。 IEmailAccount 包含与 IAccount 相同的所有属性和方法,以及一些与电子邮件相关的扩展。有没有办法用接口做类似于类继承的事情?我尝试从 IEmailAccount 中删除 2 之间共享的所有公共属性/方法,但如果我有一个 IAccounts 列表,我希望能够在该列表中包含 IEmailAccounts...
Public Interface IAccount
Property Username As String
Property Password As String
Property LoginCookie As Net.CookieContainer
Property CreationDate As Date
Property LastLoginDate As Date
Property EmailAccount As IEmailAccount
Property Proxy As HelperLib.Proxy
Sub Login()
Sub Create()
End Interface
Public Interface IEmailAccount
Property Username As String
Property Password As String
Property LoginCookie As Net.CookieContainer
Property CreationDate As Date
Property LastLoginDate As Date
Property EmailAccount As IEmailAccount
Property Proxy As HelperLib.Proxy
Property Emails As List(Of Email)
Sub Login()
Sub Create()
Sub SendMail(recipient As String, title As String, body As String)
Function GetEmails() As List(Of Email)
End Interface
Public MustInherit Class EmailAccountBase
Implements IEmailAccount
Implements IAccount
Public Property Emails As List(Of Email) Implements IEmailAccount.Emails
Public Property Username As String Implements IEmailAccount.Username, IAccount.Username
Public Property Password As String Implements IEmailAccount.Password, IAccount.Password
Public Property LoginCookie As CookieContainer Implements IEmailAccount.LoginCookie, IAccount.LoginCookie
Public Property CreationDate As Date Implements IEmailAccount.CreationDate, IAccount.CreationDate
Public Property LastLoginDate As Date Implements IEmailAccount.LastLoginDate, IAccount.LastLoginDate
Public Property EmailAccount As IEmailAccount Implements IEmailAccount.EmailAccount, IAccount.EmailAccount
Public Property Proxy As Proxy Implements IEmailAccount.Proxy, IAccount.Proxy
Public MustOverride Sub Login() Implements IEmailAccount.Login, IAccount.Login
Public MustOverride Sub Create() Implements IEmailAccount.Create, IAccount.Create
Public MustOverride Sub SendMail(recipient As String, title As String, body As String) Implements IEmailAccount.SendMail
Public MustOverride Function GetEmails() As List(Of Email) Implements IEmailAccount.GetEmails
Public Sub New(username As String, password As String)
Me.Username = username
Me.Password = password
End Sub
End Class
【问题讨论】:
-
一个接口继承另一个接口的方式与一个类继承另一个类的方式完全相同。您声明一个接口,然后在定义中添加
Inherits IBase。这正是IList继承ICollection继承IEnumerable的方式。 -
@jmcilhinney 好的,我试图改用Implements IBase...不知道我在想什么
标签: .net vb.net inheritance interface