无法获取派生类来执行功能

时间:2014-12-05 00:52:22

标签: vb.net class derived

我正在进行一项任务,我必须创建一个名为Bonus的基类。该类应包含两个公共属性:名为SalesId的String属性和名为Sales的Double属性。我还必须在类中包含默认构造函数和参数化构造函数。还包括使用以下公式计算销售员奖金的GetBonus方法(函数): 销售额* .05

然后创建一个名为PremiumBonus的派生类。派生类的GetBonu方法应按如下方式计算佣金: 销售额* .05 +(销售额 - 2500)* .01。

确保在派生类中包含默认构造函数和参数化构造函数。

这是我迄今为止所拥有的,但它只在必要时计算奖金而不是PremiumBonus。

Payroll.vb

Option Explicit On
Option Strict On
Option Infer Off

' base class
Public Class Bonus
    'declare properties
    Public Property SalesId As String
    Public Property Sales As Double

    'default constructor
    Public Sub New()
        _Sales = 0
        _SalesId = String.Empty
    End Sub

    'parameterized constructor
    Public Sub New(ByVal strId As String, ByVal dblB As Double)
        _Sales = dblB
        _SalesId = strId
    End Sub

    'method to calculate the bonus using formula
    Public Overridable Function GetBonus() As Double
        'returns the sales person's bonus
        Return _Sales * 0.05
    End Function
End Class


' derived class
Public Class PremiumBonus
    Inherits Bonus

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal dblB As Double, ByVal strId As String)
        MyBase.New(CStr(dblB), CDbl(strId))
    End Sub

    Public Overrides Function GetBonus() As Double
        Return MyBase.GetBonus + (Sales - 2500) * 0.01
    End Function
End Class

Main Form.vb

Option Explicit On
Option Strict On
Option Infer Off

Public Class frmMain

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub txtSales_Enter(sender As Object, e As EventArgs) Handles txtSales.Enter
        txtSales.SelectAll()
    End Sub

    Private Sub txtSales_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtSales.KeyPress
        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> "." AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtSales_TextChanged(sender As Object, e As EventArgs) Handles txtSales.TextChanged
        lblBonus.Text = String.Empty
    End Sub

    Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
        ' calculates and displays a bonus



        ' if the sales are over $2500, instantiate a PremiumBonus object
        ' and then calculate the bonus
        ' otherwise, instantiate a Bonus object and then calculate the bonus
        Dim myBonus As New Bonus
        Dim myPremiumBonus As New PremiumBonus
        Dim dblSales As Double

        If dblSales > 2500 Then
            Double.TryParse(txtSales.Text, myPremiumBonus.Sales)
            dblSales = myPremiumBonus.GetBonus
        Else
            Double.TryParse(txtSales.Text, myBonus.Sales)
            dblSales = myBonus.GetBonus
        End If

        ' display the bonus
        lblBonus.Text = "ID:" & (txtId.Text).PadRight(6) & dblSales.ToString("C2")
    End Sub
End Class

0 个答案:

没有答案