【问题标题】:Inherit Class with constructors in visual basic在 Visual Basic 中使用构造函数继承类
【发布时间】:2015-09-20 10:50:49
【问题描述】:

我正在使用 Visual Basic 开发一个 wpf 应用程序,我想利用继承函数来创建我的自定义类。我做了一个虚拟项目,只是为了让大家清楚我的问题。

我想从我的父类继承属性到我的子类。请注意儿童班。我的问题是,我的代码编写方式;

-当我想在代码后面的主窗口中调用子类时,我无法为子类的所有继承属性赋值,它只允许我为子类中的新属性赋值如下图。

Dim ChildClass As New ChildClass(var5, var6)'<---Unable to assing values to the rest of the inherited properties

-同样在子类中,我无法将字段分配给继承的属性,在那里你会看到这个奇怪的

MyBase.New(1, 1, 1, 1) 

这基本上阻止了我控制继承属性的值。

但是,正如您在代码后面的主窗口中看到的那样,我能够读取和使用子类的所有 6 个属性(继承的和新的),但继承的属性值并不理想。

当我从代码后面的主窗口调用子类时,我希望能够为所有 6 个属性赋值。

我想所有的构造函数定义都与这个问题有关,所以如果你能尝试运行我的程序并提出一个解决方案会很棒!

我的 MainWindow XAML 文件:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="InheritClassExample" Height="350" Width="525">
    <StackPanel>
        <TextBox Name="val1" Text="1"></TextBox>
        <TextBox Name="val2" Text="2"></TextBox>
        <TextBox Name="val3" Text="3"></TextBox>
        <TextBox Name="val4" Text="4"></TextBox>
        <TextBox Name="val5" Text="5"></TextBox>
        <TextBox Name="val6" Text="6"></TextBox>
        <Button Margin="50" Click="Button_Click">GO!</Button>
        <WrapPanel Margin="10">
            <TextBox Name="ParentClassBox" Width="auto"></TextBox>
            <TextBox Name="ChildClassBox" Width="auto"></TextBox>
        </WrapPanel>
    </StackPanel>

</Window>

我的 MainWindow 后面的代码:

 Imports DummyInheritExample.Utilities

Class MainWindow

    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
        'assign the values from my input window to varriables withing this sub
        Dim var1 As Double = val1.Text
        Dim var2 As Double = val2.Text
        Dim var3 As Double = val3.Text
        Dim var4 As Double = val4.Text
        Dim var5 As Double = val5.Text
        Dim var6 As Double = val6.Text

        'calling the parent class

        Dim parentClass As New ParentClass(var1, var2, var3, var4)
        ParentClassBox.Text = "The numerical valeus in the ParentClass are :" + parentClass.field1.ToString + parentClass.field2.ToString + parentClass.field3.ToString + parentClass.field4.ToString

        Dim ChildClass As New ChildClass(var5, var6)'<---Unable to assing values to the rest of the inherited properties
        ChildClassBox.Text = "The numerical valeus in the ChildClass are :" + ChildClass.field1.ToString + ChildClass.field2.ToString + ChildClass.field3.ToString + ChildClass.field4.ToString + ChildClass.field5.ToString + ChildClass.field6.ToString

    End Sub
End Class

我的父类的代码:

Namespace Utilities
    Public Class ParentClass

        Sub New(_field1 As Double, _field2 As Double, _field3 As Double, _field4 As Double)
            field1 = _field1
            field2 = _field2
            field3 = _field3
            field4 = _field4
        End Sub


        Private _field1 As Double

        Public Property field1() As Double
            Get
                Return _field1
            End Get
            Set(ByVal value As Double)
                _field1 = value
            End Set
        End Property

        Private _field2 As Double
        Public Property field2() As Double
            Get
                Return _field2
            End Get
            Set(ByVal value As Double)
                _field2 = value
            End Set
        End Property

        Private _field3 As Double
        Public Property field3() As Double
            Get
                Return _field3
            End Get
            Set(ByVal value As Double)
                _field3 = value
            End Set
        End Property

        Private _field4 As Double
        Public Property field4() As Double
            Get
                Return _field4
            End Get
            Set(ByVal value As Double)
                _field4 = value
            End Set
        End Property


    End Class
End Namespace

我的子类的代码:

Namespace Utilities
    Public Class ChildClass
        Inherits ParentClass

        Sub New(_field5 As Double, _field6 As Double)
            MyBase.New(1, 1, 1, 1) ' here visual basic asked me to enter this statment
            field5 = _field5
            field6 = _field6
        End Sub


        Private _field5 As Double
        Public Property field5() As Double
            Get
                Return _field5
            End Get
            Set(ByVal value As Double)
                _field5 = value
            End Set
        End Property

        Private _field6 As Double
        Public Property field6() As Double
            Get
                Return _field6
            End Get
            Set(ByVal value As Double)
                _field6 = value
            End Set
        End Property

        Dim test As Double = field1



    End Class
End Namespace

【问题讨论】:

    标签: wpf vb.net class inheritance constructor


    【解决方案1】:

    不确定这是否是您的意思 - 在 Child 类中添加一个包含所有字段的构造函数。

    Sub New(_field1 As Double, _field2 As Double, _field3 As Double, _field4 As Double, _field5 As Double, _field6 As Double)
                MyBase.New(_field1, _field2, _field3, _field4)
    

    【讨论】:

      【解决方案2】:

      是的,谢谢 Priya,谢谢,没错。我解决了我的问题,构造函数现在看起来像这样:

      Sub New(ByVal _field1 As Double, _field2 As Double, ByVal _field3 As Double, _field4 As Double, ByVal _field5 As Double, _field6 As Double)
          MyBase.New(_field1, _field2, _field3, _field4)
          Me.field5 = _field5
          Me.field6 = _field6
      
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-31
        • 1970-01-01
        • 1970-01-01
        • 2012-09-03
        • 2021-09-28
        • 2018-09-29
        • 2013-02-12
        相关资源
        最近更新 更多