如何仅使用类作为属性?

时间:2013-12-24 18:04:58

标签: vb.net class

我一直试图这么做但我找不到任何地方。我想我不是应该搜索它......

一个小例子:

Class MainClass

    Property ExampleProperty As New ExamplePropertyClass

    Private Class ExamplePropertyClass

        Sub DoSomething()

        End Sub

    End Class

End Class

在上一代码中,ExamplePropertyClass用作MainClass的属性。 始终存在错误,指出我无法将私有类公开为

但是如何才能使“可见”属性成为可能,我的意思是要使用代码的用户应该只使用property而不是class,{怎么可以{ {1}}不是class还是可见?

2 个答案:

答案 0 :(得分:0)

什么是属性实际上是setter和getter的语法糖。所以主要是默认的公共 您将类声明为私有。所以外面看不见它。然后如果不可见那么就会发生冲突然后人们如何知道在不知道其类型的情况下分配和获取该对象。所以该类型应该是公开的和可见的

dim m as new MainClass()
m.ExampleProperty=? ' What is ExampleProperty ?int , object. So it should not be unknown

另一种声称你不会在外面使用该属性的方式。这样就可以在里面安装私人课程了。

'Explicitly make property to be used only within class
Private Property ExampleProperty As ExamplePropertyClass 

答案 1 :(得分:0)

您可以使用接口执行此操作:

Public Interface IDoesSomething
    Sub DoSomething()
End Interface


Public Class MainClass

    Public Sub New()
        m_example = New InternalClass
    End Sub

    Private m_example As IDoesSomething
    Public ReadOnly Property Example() As IDoesSomething
        Get
            Return m_example
        End Get
    End Property


    Private Class InternalClass
        Implements IDoesSomething


        Public Sub DoSomething() Implements IDoesSomething.DoSomething

        End Sub
    End Class

End Class
相关问题