在C#上定义方法的正确方法

时间:2019-03-03 00:05:07

标签: c#

大家好,我是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");

        }
    }

所以我希望屏幕上显示以下结果

1 John Doe每月赚取20000 2 Jane Doe的月收入为28800

但是我得到

1 John Doe每月赚取20000 2 Jane Doe的月薪为36000

我不知道如何定义方法,或者可能还有其他错误,任何人都想分享其知识。

谢谢。

4 个答案:

答案 0 :(得分:2)

您的代码无法编译。
无法编译调用 Employee.getNetSalary 的方法,因为 getNetSalary 是一个实例方法,不能被称为静态方法。您需要Employee类的实例来调用它。

要解决您的问题,您需要将调用移至方法 printInformation 中的方法 getNetSalary ,并在调用它时使用对当前实例(this)的引用

这是您修改后的 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 < / p>

public Employee(int employeeId, string fullName, float salary, bool taxDeducted = true)
{
    this.employeeId = employeeId;
    this.fullName = fullName;
    this.salary = salary;
    this.taxDeducted = taxDeducted;
}

答案 1 :(得分:0)

简单的解决方法是删除此行

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");
}

或只计算一次工资以提高绩效

答案 2 :(得分:0)

您可以通过这种方式减少班级。

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个John Doe的月收入为20000

2 Jane Doe的月收入为28800

答案 3 :(得分:0)

我将字段更改为自动属性。如果您需要将您的班级绑定到其他应用程序,这将有所帮助。我将方法和公共属性的首字母大写以使VS满意。我将GetNetSalary设为私有。在类之外无法访问它,我用它来设置一个新属性NetSalary。在构造函数中调用它。我还在类中摆脱了Console.WriteLine并提供了.ToString的替代。这样可以将类与用户界面断开连接,并允许该类迁移到其他类型的应用程序。

((p->getNext())->getPrev())->setNext(p->getPrev());

}