我不能继承我父类的形式

时间:2015-11-03 08:31:15

标签: vb.net

我遇到了一个我无法解决的问题,我正在尝试使用vb.net 2013编写工资单系统代码,我必须使用控制台应用程序。

我正在使用课程来计算工资,具体取决于员工工作的周数,但问题是我不能从我的父课继承,我继续得到这个错误,我不知道如何解决它。

Error 2 Class 'Employee_Payment_System.Employee' has no accessible 'Sub New' and cannot be inherited.

每当我尝试添加'sub new'时,我都会得到这个

Error 3 'Public Sub New()' has multiple definitions with identical signatures.  

到目前为止,这是我的代码

Public Class Employee

#Region "Private Declarations"

    Private EMP_FullName As String
    Private EMP_LastName As String
    Private EMP_Salary As Double
    Private EMP_Number As Integer
    Private EMP_Address As String

#End Region
'The values are obtained from the public properties  coded below 
    Private Sub New()
        EMP_FullName = ""
        EMP_LastName = ""
        EMP_Salary = 0.0
        EMP_Address = ""
    End Sub

    Private Sub New(ByVal FullName As String, ByVal LastName As String, ByVal Address As String, ByVal Salary As Double, ByVal Number As Integer)

        EMP_FullName = FullName
        EMP_LastName = LastName
        EMP_Address = Address
        EMP_Number = Number
        EMP_Salary = Salary

    End Sub
' the properties gets their values from the console
    Public Property FullName() As String
        Get
            Return EMP_FullName
        End Get
        Set(value As String)
            EMP_FullName = value
        End Set
    End Property

    Public Property LastName() As String
        Get
            Return EMP_LastName
        End Get
        Set(value As String)
            EMP_LastName = value
        End Set
    End Property

    Public Property Address() As String
        Get
            Return EMP_Address
        End Get
        Set(value As String)
            EMP_Address = value
        End Set
    End Property

    Public Property Salary() As Double
        Get
            Return EMP_Salary
        End Get
        Set(value As Double)
            EMP_Salary = value
        End Set
    End Property

    Public Property Number() As Integer
        Get
            Return EMP_Number
        End Get
        Set(value As Integer)
            EMP_Number = value
        End Set
    End Property

    Public Overridable Function Payment() As Double
        Return EMP_Salary
    End Function
End Class

Public Class WeeklySalary : Inherits Employee
'The main problem "Error 2  Class 'Employee_Payment_System.Employee' has no accessible 'Sub New' and cannot be inherited." generates here

    Sub New()' I get my second error "Error 3 'Public Sub New()' has multiple definitions with identical signatures.    " here

    End Sub

    Private NumberOfWeeks As Integer

    Private Sub New() 'The second Error is related to this sub
        NumberOfWeeks = 0
    End Sub
    Public Sub New()
        NumberOfWeeks = Number
    End Sub
    Public Property Number_Of_Weeks() As Integer 'This is the number of weeks worked
        Get
            Return NumberOfWeeks
        End Get
        Set(value As Integer)
            NumberOfWeeks = value
        End Set
    End Property
    Public Overrides Function Payment() As Double

        Return NumberOfWeeks * Salary 'Calculates Weekly salary here

    End Function
End Class

我仍然需要为每月付款再编写一个类,我仍然需要为控制台界面编写模块代码,但我需要我的类才能启动模块(出于测试目的)。

3 个答案:

答案 0 :(得分:1)

好的,我已经成功解决了这个问题,我已经将我的私有构造函数更改为public并且它有效了谢谢

答案 1 :(得分:0)

正如jonrsharpe所说:你为什么要把你的构造函数(Sub New)私有化?添加没有参数的Public Sub New时,它会与Private Sub New()发生冲突。您不能使用具有相同名称和足迹的方法。 但是你想通过从Employee继承来实现什么?如果您尝试执行一些EmployeeWorkRecord类,它应该聚合Employee和工作记录而不是从它继承。

答案 2 :(得分:0)

我不是把它写成批评,而是作为关于继承的一般指针。

继承的想法是子类与父类是同一种对象,但具有额外的功能。

要进行真实的世界比较,某人的工资与某人的工资不同。

但是你可以拥有一个类Human。子类可以是雇员,而另一个子类可以是客户。每个都有自己的属性。

员工可以拥有StartOfEmployment财产,而客户可以拥有LoyaltyCardPoints属性,他们可以拥有ContactAddress和Gender。

反正。答案 - 或任何一种可能的答案

我怀疑你是以错误的方式看待继承。 员工可能会继承WeeklySalary类,而一类Manager可能会继承MonthSalary类。但您可能最好使用员工编号作为关键设置某种数据表。如果你按照你现在的方式这样做,你的程序中会有很多重复或未使用的数据。

父母和子女课程的命名有时是反直觉的,因为我们经常认为儿童比父母小,但在编程中,孩子与父母相同,但具有额外的功能。