我正在使用Visual Basic编写一个wpf应用程序,我想使用继承函数来创建我的自定义类。我做了一个虚拟项目,只是为了让每个人都明白我的问题。
我想从父类继承属性到我的子类。请注意孩子班。我的问题在于编写代码的方式;
- 当我想在代码后面的mainwindow中调用子类时,我无法为子类的所有固有属性赋值,它只允许我为子类中的新属性赋值如下图所示。
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
答案 0 :(得分: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)
答案 1 :(得分:0)
是的,谢谢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