如何从子类(在嵌套类中)获取父类中的值?

时间:2021-11-01 23:28:57

I have Class1 and class2 which is inside class1, VB.NET code:

我有Class1和class2在Class1, VB里面。NET代码:

Public Class class1
    Public varisbleX As Integer = 1
    Public Class class2
        Public Sub New()
            'Here GET the value of VariableX
        End Sub
    End Class

    Public Sub New()
        Dim cls2 As New class2
    End Sub
End Class

I want to access varisbleX from class2, code in VB.net or C# is appreciated, Thanks.

我想从class2访问varisbleX,非常感谢VB.net或c#中的代码,谢谢。

2 个解决方案

#1


8  

The inner class (class2) is not associated with any specific instance of the outer class (class1). T access fields etc, you will need to first have an explicit reference to a class1 instance, probably passing it in via the constructor. For example, it could be:

内部类(class2)与外部类的任何特定实例(class1)没有关联。T访问字段等等,首先需要有一个对class1实例的显式引用,可能是通过构造函数传入的。例如,它可以是:

Public Class class1
    Public varisbleX As Integer = 1
    Public Class class2
        Public Property Parent As class1

        Public Sub New(oParent As class1)
            Me.Parent = oParent
            Console.WriteLine(oParent.varisbleX)
        End Sub
    End Class

    Public Sub New()
        Dim cls2 As New class2(Me)
    End Sub
End Class

#2


0  

If you only need a few variables you can pass the variable(s) as a parameter when initializing Class2.

如果只需要几个变量,可以在初始化Class2时将变量作为参数传递。

Public Class Class1

    Public VariableX As Integer = 1

    Public Class Class2
        Public Sub New(ByVal VariableX As Integer)
            'Here GET the value of VariableX
            Debug.Print(VariableX)
        End Sub
    End Class

    Public Sub New()
        Dim cls2 As New Class2(VariableX)
    End Sub

End Class

This way Class2 doesn't have access to all of Class1's variables and properties; only what you explicitly give it. Usually we don't want the child class to be in control of the parent class. So this method provides that separation.

这样,Class2就不能访问Class1的所有变量和属性;只有你明确给出的。通常我们不希望子类控制父类。这个方法提供了分离。

#1


8  

The inner class (class2) is not associated with any specific instance of the outer class (class1). T access fields etc, you will need to first have an explicit reference to a class1 instance, probably passing it in via the constructor. For example, it could be:

内部类(class2)与外部类的任何特定实例(class1)没有关联。T访问字段等等,首先需要有一个对class1实例的显式引用,可能是通过构造函数传入的。例如,它可以是:

Public Class class1
    Public varisbleX As Integer = 1
    Public Class class2
        Public Property Parent As class1

        Public Sub New(oParent As class1)
            Me.Parent = oParent
            Console.WriteLine(oParent.varisbleX)
        End Sub
    End Class

    Public Sub New()
        Dim cls2 As New class2(Me)
    End Sub
End Class

#2


0  

If you only need a few variables you can pass the variable(s) as a parameter when initializing Class2.

如果只需要几个变量,可以在初始化Class2时将变量作为参数传递。

Public Class Class1

    Public VariableX As Integer = 1

    Public Class Class2
        Public Sub New(ByVal VariableX As Integer)
            'Here GET the value of VariableX
            Debug.Print(VariableX)
        End Sub
    End Class

    Public Sub New()
        Dim cls2 As New Class2(VariableX)
    End Sub

End Class

This way Class2 doesn't have access to all of Class1's variables and properties; only what you explicitly give it. Usually we don't want the child class to be in control of the parent class. So this method provides that separation.

这样,Class2就不能访问Class1的所有变量和属性;只有你明确给出的。通常我们不希望子类控制父类。这个方法提供了分离。