为什么我的 get/set 方法不返回值?

时间:2021-04-24 15:19:22

标签: c#

我有一项用 C# 编写的任务,即为员工、学生和讲师创建一个类。

除了两个不常见的字段外,每个部分都有一个共同的字段。 我做了 get/set 的代码,如果值低于零,它应该打印一个零,但每次我运行控制台时它都不会打印预期值,例如:

如果用户输入 -8,它会打印 -8 而不是 0。

class Program
{
    public class Data
    {
        public string _FirstName;
        public string _LastName;
        public int _ID;
        public string _Address;

        public Data(string Fn, string Ln, int Id, string Add)
        {
            _FirstName = Fn;
            _LastName = Ln;
            _ID = Id;
            _Address = Add;
        }

        public void Print()
        {
            Console.WriteLine($"First Name: {_FirstName}\nLast Name: {_LastName}\nID: {_ID}\nAddress: {_Address}");
        }
    }

    public class Employee : Data
    {
        public float _Salary;
        public int _Seniority;

        public Employee(string Fn, string Ln, int Id, string Add, float Sal, int Sen) : base(Fn, Ln, Id, Add)
        {
            _Salary = Sal;
            _Seniority = Sen;
        }

        public float SalCheck
        {
            set 
            {
                if (this._Salary < 0)
                    _Salary = 0;
                else
                    _Salary = value;
            }
            get
            {
                return _Salary;
            }
        }

        public int SenCheck
        {
            set
            {
                if (value < 0)
                    _Seniority = 0;
                else
                    _Seniority = value;
            }
            get
            {
                return _Seniority;
            }
        }

        public void PrintE()
        {
            Console.WriteLine($"Salary: {SalCheck}\nSeniority: {SenCheck}");
        }

    static void Main(string[] args)
    {
        Console.WriteLine("Press 1 for Employee\nPress 2 for Student\nPress 3 for Lecturer");
        int num = int.Parse(Console.ReadLine());

        switch (num)
        {
            case 1:
                Console.WriteLine("Employee Data");
                Console.Write("Enter First Name: ");
                string FnE = Console.ReadLine();
                Console.WriteLine();
                Console.Write("Enter Last Name: ");
                string LnE = Console.ReadLine();
                Console.WriteLine();
                Console.Write("Enter ID: ");
                int IdE = int.Parse(Console.ReadLine());
                Console.WriteLine();
                Console.Write("Enter Address: ");
                string AdE = Console.ReadLine();
                Console.WriteLine();
                Console.Write("Enter Salary: ");
                int SalE = int.Parse(Console.ReadLine());
                Console.WriteLine();
                Console.WriteLine("Enter Seniority: ");
                int SenE = int.Parse(Console.ReadLine());
                Employee emplo = new Employee(FnE, LnE, IdE, AdE, SalE, SenE);
                emplo.Print();
                emplo.PrintE();
                break;
     }
}

上面的代码是我所做的,但如果它低于零,它不会打印零值。

enter image description here

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

Console.WriteLine("Employee Data");
                    Console.Write("Enter First Name: ");
                    string FnE = Console.ReadLine();
                    Console.WriteLine();
                    Console.Write("Enter Last Name: ");
                    string LnE = Console.ReadLine();
                    Console.WriteLine();
                    Console.Write("Enter ID: ");
                    int IdE = int.Parse(Console.ReadLine());
                    Console.WriteLine();
                    Console.Write("Enter Address: ");
                    string AdE = Console.ReadLine();
                    Console.WriteLine();
                    Console.Write("Enter Salary: ");
                    int SalE = int.Parse(Console.ReadLine());
                    Console.WriteLine();
                    Console.Write("Enter Seniority: ");
                    int SenE = int.Parse(Console.ReadLine());
                    Employee emplo = new Employee(FnE, LnE, IdE, AdE, SalE, SenE);
                    emplo.Print();
                    emplo.SalCheck = SalE;
                    emplo.SenCheck = SenE;
                    emplo.PrintE();

我只需要放最后两行代码

emplo.SalCheck = SalE;
emplo.SenCheck = SenE;

而且效果很好!!