抽象类 工资的计算方法是将每日费率乘以一个月的工作天数。 我试图计算计算员工工资的工资和计算扣除工资和净工资扣除的扣除额 例如:医生= 10000,老师= 20000,工程师= 30000
试图获得输出 //
ID 001 Name J Salary 15750.00 Deduction 3386.25 Net Pay 12363.75
namespace Practice
{
////////////////////////////////////////////
////////////////////////////////////////////
///////////// class employee ///////////////
////////////////////////////////////////////
////////////////////////////////////////////
abstract class Employee
{
int employeeid;
string name;
double salary, deduction, daysworked;
public Employee()
{
}
public int Employeeid
{
get { return employeeid; }
set { employeeid = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public double Daysworked
{
get { return daysworked; }
set { daysworked = value; }
}
public double Deduction
{
get { return deduction; }
set { deduction = value; }
}
public double Salary
{
get { return salary; }
set { salary = value; }
}
public virtual double calculatesalary()
{
return Salary * Daysworked;
}
public virtual double calulatededuction()
{
return calculatesalary() * Deduction;
}
}
////////////////////////////////////////////
////////////////////////////////////////////
///////////// class sample1 ///////////////
////////////////////////////////////////////
////////////////////////////////////////////
class sample1 : Employee
{
public sample1 (string name,int employeeid, double daysworked,double salary)
{
Name = name;
Employeeid = employeeid;
Daysworked = daysworked;
Salary = salary;
}
public override double calculatesalary()
{
if (calculatesalary() <= 10000)
{
Deduction = 0.11;
}
if (calculatesalary() >= 10001 && calculatesalary() <= 20000)
{
Deduction = 0.22;
}
if (calculatesalary() >= 20001 && calculatesalary() <= 30000)
{
Deduction = 0.34;
}
if (calculatesalary() >= 30001)
{
Deduction = 0.58;
}
return calculatesalary() * Deduction;
}
}
}
我的问题是我哪里弄错了? :/
答案 0 :(得分:0)
正如John Wu指出的那样,它以无限循环结束,因为你以递归方式调用calculatesalary()
类中的sample1
方法。使用base.calculatesalary()
将调用基本方法,然后在派生类中执行其他逻辑。修改您的方法如下:
public override double calculatesalary()
{
var salary = base.calculatesalary();
if (salary <= 10000)
{
Deduction = 0.11;
}
else if (salary >= 10001 && salary <= 20000)
{
Deduction = 0.22;
}
else if (salary >= 20001 && salary <= 30000)
{
Deduction = 0.34;
}
else if (salary >= 30001)
{
Deduction = 0.58;
}
return salary * Deduction;
}
请考虑重构此类,因为它需要一些修复,如Dan Chase所述。