【发布时间】:2016-07-03 08:03:19
【问题描述】:
我有一个场景,我的基类 (Employee) 包含很少的属性,并且它有两个子类(PermanentEmployee 和 ContractEmployee)。现在有一个属性,它只是 PermanentEmployee (MedicalCoverage) 的一部分。而且我正在使用多态性在运行时决定员工的类型,因此我需要将 MedicalCoverage 属性作为基类的一部分,以便我可以在访问员工对象的客户端代码中访问它。我不希望 ContractEmployee 拥有这个属性那么如何才能实现这个或这个设计的任何替代方案。
基类员工长这样
Public Class Employee
Public EmployeeId As Integer
Public EmployeeFirstName As String
Public EmployeeLastName As String
Public Salary As Integer
Public Overridable ReadOnly Property bonus As Double
Get
Return Salary
End Get
End Property
End Class
我想让 MedicalCoverage 成为 PermanentEmployee 的一部分
Public Class PermanantEmployee
Inherits Employee
Public Overrides ReadOnly Property bonus As Double
Get
Return 15 * Salary
End Get
End Property
Public Property MedicalCoverage As Double 'The property to be accessed only here
End Class
合同雇员是这样的
Public Class ContractEmployee : Inherits Employee
Public Overrides ReadOnly Property bonus As Double
Get
Return 12 * Salary
End Get
End Property
'The MedicalCoverage property is not part of the class here
End Class
【问题讨论】:
标签: .net vb.net oop polymorphism