gpt4 book ai didi

vb.net - 如何在父类中使用子类的值?网络

转载 作者:行者123 更新时间:2023-12-02 02:17:09 25 4
gpt4 key购买 nike

我有一个令我沮丧不已的问题,我在父类中有一个可重写的函数,在子类中有一个重写函数,如下所示:

子类

Public Overrides Sub UpdatePrice(ByVal dblRetailPrice As Double)
If dblWholesalePrice < 0 Then
MessageBox.Show("Error, amount must be greater than 0.")
Else
dblRetailPrice = dblWholesalePrice * dblStandardMargin
End If
End Sub

在父类中,我有

Public ReadOnly Property RetailPrice() As Double
Get
Return dblRetailPrice
End Get
End Property

Public Overridable Sub UpdatePrice(ByVal dblRetailPrice As Double)
If dblWholesalePrice < 0 Then
MessageBox.Show("Please input an amount greater than 0,wholesale price has not changed", "error")
Else
dblRetailPrice = 1.1 * dblWholesalePrice
End If
End Sub

当我调试时,产生了值,但它没有传递给 ski.RetailPrice() 的父类,这里似乎有什么问题?任何帮助将不胜感激。

最佳答案

您不应在更高范围内传递与类级变量同名的参数。局部变量将覆盖另一个,这意味着您的 setter 中的此语句:

dblRetailPrice = 1.1 * dblWholesalePrice

将设置您刚刚传入的 dblRetailPrice 临时参数的值,而不是您的类级 dblWholesalePrice 成员变量。

简单的解决方案是通过删除无用的类型符号前缀来更改参数的名称:

Public Class MyClass

Protected dblRetailPrice As Double

Public ReadOnly Property RetailPrice() As Double
Get
Return dblRetailPrice
End Get
End Property

Public Overridable Sub UpdatePrice(ByVal retailPrice As Double)

If dblWholesalePrice < 0 Then
MessageBox.Show("Please input an amount greater than 0,wholesale price has not changed", "error")
Else
dblRetailPrice = 1.1 * dblWholesalePrice
End If
End Sub

End Class

关于vb.net - 如何在父类中使用子类的值?网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9953319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com