0

请看下面的代码:

'Form1.vb
Imports WindowsApplication1.BusinessLogicLayerShared

Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim IPerson As IPerson
        IPerson = New BusinessLogicLayer.Person()
        Dim name As String = IPerson.getName()
    End Sub
End Class

'Person.vb
Imports WindowsApplication1.BusinessLogicLayerShared

Namespace BusinessLogicLayer
    Public Class Person
        Implements IPerson

        Private IPerson As DataLogicLayerShared.IPerson

        Public Function getName() As String Implements IPerson.getName
            IPerson = New DataLogicLayer.Person
            getName = IPerson.getName
        End Function
    End Class
End Namespace

Namespace BusinessLogicLayerShared
    Public Interface IPerson
        Function getName() As String
    End Interface
End Namespace

'Person.vb
Imports WindowsApplication1.DataLogicLayerShared
Namespace DataLogicLayer

    Public Class Person
        Implements IPerson

        Public Function getName() As String Implements IPerson.getName
            'Connect to database and get name
            Return "Ian"
        End Function

        Public Function getAge() Implements IPerson.getAge

        End Function
    End Class
End Namespace

Namespace DataLogicLayerShared
    Public Interface IPerson
        Function getName() As String
        Function getAge()
    End Interface
End Namespace

DataLogicLayer.Person 中的所有函数都是公共的。这不违反封装规则吗?有没有解决的办法?代码重用和封装之间似乎存在折衷。

4

1 回答 1

3

否 - 如果这些方法正在实现您的接口,则它们必须是公开的。如果您有一些内部运作GetAgeGetName您不必要地允许公开,那将违反规则。

于 2013-01-17T11:23:33.830 回答