嗨,伙计们,我在C#上很新,我想让这段代码以正确的方式工作,并理解为什么没有以正确的方式运行。
所以我有这个。
class PayrollRunner
{
static void Main(string[] args)
{
// use Employee without tax
Employee john = new Employee(1, "John Doe", 20000, false);
john.printInformation();
// use Employee with tax
Employee jane = new Employee(2, "Jane Doe", 36000);
jane.printInformation();
// use WeeklyEmployee without tax
// WeeklyEmployee jack = new WeeklyEmployee(3, "Jack Deer", 18500, false);
//jack.printInformation();
// use WeeklyEmployee with tax
//WeeklyEmployee jen = new WeeklyEmployee(4, "Jen Deer", 18000);
// jen.printInformation();
Console.Read();
}
}
class Employee
{
private int employeeId;
private string fullName;
private float salary;
private bool taxDeducted;
public Employee(int employeeId, string fullName, float salary, bool taxDeducted)
{
this.employeeId = employeeId;
this.fullName = fullName;
this.salary = salary;
this.taxDeducted = taxDeducted;
}
public Employee(int employeeId, string fullName, float salary)
{
this.employeeId = employeeId;
this.fullName = fullName;
this.salary = salary;
this.taxDeducted = true;
}
public float getNetSalary()
{
float netSalary;
float tax = 0.8;
if (taxDeducted)
{
netSalary = salary * tax;
}
else
{
netSalary = salary;
}
return netSalary;
}
float netSalary = Employee.getNetSalary();
public void printInformation()
{
Console.WriteLine(employeeId + " " + fullName + " earns " +
netSalary + " per month");
}
}因此,我期望在屏幕上得到以下结果
无名氏每月可赚取20000元2简无名氏月入28800元
但我得到了
无名氏每月可赚取20000元2简无名氏月入36000元
我不知道如何定义这个方法,或者可能有其他错误,任何人都愿意分享它的知识。
谢谢。
发布于 2019-03-03 00:11:01
简单的修正是删除这一行。
float netSalary = Employee.getNetSalary();然后用这个
public void printInformation()
{
Console.WriteLine(employeeId + " " + fullName + " earns " +
getNetSalary() + " per month");
}或者创建一个属性而不是方法getNetSalary()
public float NetSalary
{
get
{
float netSalary;
float tax = 0.8;
if (taxDeducted)
{
netSalary = salary * tax;
}
else
{
netSalary = salary;
}
return netSalary;
}
}然后
public void printInformation()
{
Console.WriteLine(employeeId + " " + fullName + " earns " +
NetSalary + " per month");
}或计算此薪资一次以提高绩效
发布于 2019-03-03 00:13:44
您的代码不编译。
无法编译调用Employee.getNetSalary,因为getNetSalary是实例方法,不能作为静态方法调用。您需要雇员类的一个实例来调用它。
要解决问题,需要在方法getNetSalary中移动对方法printInformation的调用,并在调用它时使用对当前实例(此)的引用。
这是你修改过的printInformation
public void printInformation()
{
// Call on the class instance properties to execute the calc
float netSalary = this.getNetSalary();
Console.WriteLine($"{employeeId} {fullName} earns {netSalary:C} per month");
}通过这种方式,getNetSalary使用员工当前实例的属性工作。
我建议的第二个技巧是删除第二个构造函数(不使用taxDeducted布尔值的构造函数),但只编写一个构造函数,将taxDeducted属性的默认值设置为true
public Employee(int employeeId, string fullName, float salary, bool taxDeducted = true)
{
this.employeeId = employeeId;
this.fullName = fullName;
this.salary = salary;
this.taxDeducted = taxDeducted;
}发布于 2019-03-03 01:00:09
你可以这样减少这门课。
class Employee
{
private int employeeId;
private string fullName;
private float salary;
private bool taxDeducted;
public Employee(int employeeId, string fullName, float salary, bool taxDeducted=true)
{
this.employeeId = employeeId;
this.fullName = fullName;
this.salary = salary;
this.taxDeducted = taxDeducted;
}
public float getNetSalary()
{
float tax = 0.8F;
float salary = this.salary;
if (taxDeducted)
salary *= tax;
return salary;
}
public void printInformation()
{
Console.WriteLine(employeeId + " " + fullName + " earns " + getNetSalary() + " per month");
}
}那就试试吧
Employee john = new Employee(1, "John Doe", 20000, false);
john.printInformation();
// use Employee with tax
Employee jane = new Employee(2, "Jane Doe", 36000);
jane.printInformation();1无名氏月入20000元
2无名女尸每月可赚取28800元
https://stackoverflow.com/questions/54964313
复制相似问题