如何在C#中显示setter和getter并将它们声明给Main方法

如何在C#中显示setter和getter并将它们声明给Main方法,c#,C#,如何在main方法中声明此代码。我的偏好主要是在类中进行计算和构造,并且只在main中声明类 namespace Namsolution_Payroll_System { class Program { static void Main(string[] args) { ITDepartment emp = new ITDepartment(); emp.DisplayITEmployee();

如何在main方法中声明此代码。我的偏好主要是在类中进行计算和构造,并且只在main中声明类

namespace Namsolution_Payroll_System
{
    class Program
    {
        static void Main(string[] args)
        {

            ITDepartment emp = new ITDepartment();
            emp.DisplayITEmployee();
            Console.ReadKey();
        }
    }

    public class Employee
    {
        //please review other data types you can use before submitting: Self note!!
        public string _first_Name;
        public string _last_Name;
        public DateTime  _date_Of_Birth;
        public string _gender;                               // (male or female columes) : Self Note
        public double _staff_Number;                            //this should be unique
        public string _department;
        public DateTime _date_Of_Employment;
        public string _position;                                     //rank

        //Code set to Variables
        public void SetFirstName(string employeeName)
        {
            _first_Name = employeeName;
        }
        public void SetLastName(string employeeLastName)
        {
            _last_Name = employeeLastName;
        }
        public void SetdateOfBirth(DateTime employeeDateOfBith)
        {
            _date_Of_Birth = employeeDateOfBith;
        }
        public void SetGender(string employeeGender)
        {
            _gender = employeeGender;
        }
        public void SetStaffNumber(double employeeStaffNumber)
        {
            _staff_Number = employeeStaffNumber;
        }
        public void SetDepartment(string employeeDepartment)
        {
            _department = employeeDepartment;
        }
        public void SetDateOfEmployment(DateTime employeedateOfEmployment)
        {
            _date_Of_Employment = employeedateOfEmployment;
        }
        public void SetPosition(string employeePositon)
        {
            _position = employeePositon;
        }

        //Input Code
        public string GetFirstName()
        {
            return _first_Name;
        }
        public string GetLastTName()
        {
            return _last_Name;
        }
        public DateTime GetDateOfBirth()
        {
            return _date_Of_Birth;
        }
        public string GetGender()
        {
            return _gender;
        }
        public double GetStaffNumber()
        {
            return _staff_Number;
        }
        public string GetDepartment()
        {
            return _department;
        }
        public DateTime GetDateOfEmployment()
        {
            return _date_Of_Employment;
        }
        public string GetPosition()
        {
            return _position;
        }

        public void DisplayEmployee()
        {
            Console.WriteLine("This is from Employee class");
        }

    }





    class ITDepartment : Employee
    {
        public void DisplayITEmployee()
        {
            DisplayEmployee();
            Console.WriteLine("This is from ITDepartment class");
        }
    }
}

问题不清楚,我已经编了个例子。根据你的问题,我建议你制作一个员工并展示它。但是这必须在类中完成,而不是在主方法中。下面是一个创建员工并将其输出到控制台的示例

属性只是一个简写,它将在后台创建一个公共get方法、一个公共set方法和一个私有字段来存储值。因此,在代码中可以使用单个属性,而不是手动添加私有字段和get/set方法

 // example property
 public string Name { get; set; }
属性与下面的代码相同。但是您可以在一行中指定它。私有字符串名称

 public string GetName(){
    return this.Name;
 }

 public string SetName(string name){
    this.Name = name;
 }
所以在这段代码中,我按照您的要求使用属性s。我已将集合设置为私有,因为字段不需要在类外偶然出现

class Program
{
    public static ItDepartment ItDepartment = new ItDepartment();
    static void Main(string[] args)
    {
        while (true)
        {
            Console.WriteLine("Press l for emoloyee list");
            Console.WriteLine("Press a for adding a employee");
            Console.WriteLine("Press x for closing program");

            // Get pressed key and take action
            switch (Console.ReadKey().KeyChar)
            {
                case 'a':
                    Console.Clear();
                    ItDepartment.Add();
                    break;
                case 'l':
                    Console.Clear();
                    ItDepartment.PrintItDepartmentEmployees();
                    break;
                case 'x':
                    return;
                default:
                    Console.WriteLine("Not a vallid key");
                    break;
            }
        }
    }
}

public class Employee
{
    public string FirstName { get; private set; }
    public string LastName { get; private set; }
    public DateTime BirthDate { get; private set; }
    public string Gender { get; private set; }
    public double StaffMenember { get; private set; }
    public DateTime DateOfEmployment { get; private set; }
    public string Position { get; private set; }
    public string Department { get; private set; }

    public void SetUser()
    {
        Console.WriteLine("Enter First Name for employee");
        FirstName = Console.ReadLine();
        Console.WriteLine("Enter Last Name for employee");
        LastName = Console.ReadLine();
        Console.WriteLine("Enter department for employee");
        Department = Console.ReadLine();
        Console.WriteLine("Enter position for employee");
        Position = Console.ReadLine();
        Console.WriteLine("Enter BirthDate for employee");
        BirthDate = GetDate();
        Console.WriteLine("Enter DateOfEmployment for employee");
        DateOfEmployment = GetDate();
        Console.WriteLine("Enter gender for employee");
        Gender = Console.ReadLine();
        StaffMenember = Program.ItDepartment.TotalEmployees();
    }

    private DateTime GetDate()
    {
        // 1. Ask the user to enter a date. 
        // 2. If it can't be converted to date like '132' it will try again and ask for the date ect..
        // 3. If success return date
        DateTime time;
        int counter = 0;
        do
        {
            if (counter != 0)
            {
                Console.WriteLine("Could not convert input to date try again...");
            }
            time = StringToDate(Console.ReadLine());
            counter++;
        } while (time == DateTime.MinValue);
        return time;
    }
    private DateTime StringToDate(string input)
    {
        // 1. Get string input and try to convert it to a date.
        // 2. If it fails return the minimum version of datetime so we know it is wrong and can't be parsed
        return DateTime.TryParse(input, out DateTime date) ? date : DateTime.MinValue;
    }

    public override string ToString()
    {
        // Return a string of all the fiels in the current employee
        return $"FirstName: {FirstName}\nLastName: {LastName}\nBirthDate: {BirthDate}\nGender: {Gender}\nStaffMenember: {StaffMenember}\nDateOfEmployment: {DateOfEmployment}\nPosition: {Position}\nDepartment: {Department}\n";
    }

}

class ItDepartment
{
    private List<Employee> _employees;

    public ItDepartment()
    {
        _employees = new List<Employee>();
    }

    public void Add()
    {
        // Create employee
        Employee emp = new Employee();
        // Call the method SetUser in Employee class
        emp.SetUser();
        // When the information is set add the employee object to the list
        _employees.Add(emp);
    }

    public void PrintItDepartmentEmployees()
    {
        // loop thrue all employees in the list and call method to string withs will return a string of the employee.
        foreach (var emp in _employees)
        {
            Console.WriteLine("================================================================================");
            // Write the string to the console.
            Console.WriteLine(emp.ToString());
        }
    }

    public int TotalEmployees()
    {
        // returns the total of employees in the list for the staff member.
        return _employees.Count;
    }
}
类程序
{
公共静态IT部门IT部门=新IT部门();
静态void Main(字符串[]参数)
{
while(true)
{
Console.WriteLine(“按l键查看emoloyee列表”);
Console.WriteLine(“按a添加员工”);
Console.WriteLine(“按x键关闭程序”);
//按下该键并采取行动
开关(Console.ReadKey().KeyChar)
{
案例“a”:
Console.Clear();
ItDepartment.Add();
打破
案例“l”:
Console.Clear();
ItDepartment.PrintItDepartmentEmployees();
打破
案例“x”:
返回;
违约:
控制台。WriteLine(“不是vallid键”);
打破
}
}
}
}
公营雇员
{
公共字符串名{get;private set;}
公共字符串LastName{get;private set;}
公共日期时间出生日期{get;private set;}
公共字符串{get;private set;}
公共双StaffMenember{get;private set;}
public DateTime dateof employment{get;private set;}
公共字符串位置{get;private set;}
公共字符串部门{get;private set;}
公共void SetUser()
{
Console.WriteLine(“输入员工的名字”);
FirstName=Console.ReadLine();
Console.WriteLine(“输入员工的姓氏”);
LastName=Console.ReadLine();
Console.WriteLine(“为员工输入部门”);
Department=Console.ReadLine();
Console.WriteLine(“为员工输入职位”);
位置=Console.ReadLine();
Console.WriteLine(“输入员工的生日”);
生日=GetDate();
Console.WriteLine(“输入员工的雇用日期”);
DateOfEmployment=GetDate();
Console.WriteLine(“为员工输入性别”);
Gender=Console.ReadLine();
StaffMenember=Program.ItDepartment.TotalEmployees();
}
private DateTime GetDate()
{
//1.要求用户输入日期。
//2.如果无法将其转换为“132”之类的日期,它将重试并询问日期等。。
//3.如果成功返回日期
日期时间;
int计数器=0;
做
{
如果(计数器!=0)
{
WriteLine(“无法将输入转换为日期,请重试…”);
}
time=StringToDate(Console.ReadLine());
计数器++;
}while(time==DateTime.MinValue);
返回时间;
}
私有日期时间StringToDate(字符串输入)
{
//1.获取字符串输入并尝试将其转换为日期。
//2.如果失败,返回datetime的最小版本,这样我们就知道它是错误的,无法解析
return DateTime.TryParse(input,out DateTime date)?date:DateTime.MinValue;
}
公共重写字符串ToString()
{
//返回当前员工中所有字段的字符串
return$“FirstName:{FirstName}\n LastName:{LastName}\n出生日期:{BirthDate}\n性别:{Gender}\n员工:{staffmenemenber}\n职位:{Position}\n部门:{Department}\n”;
}
}
班级信息技术部
{
私人名单(员工),;
公共资讯科技署()
{
_雇员=新名单();
}
公共无效添加()
{
//创建员工
员工emp=新员工();
//在Employee类中调用方法SetUser
emp.SetUser();
//设置信息后,将employee对象添加到列表中
_添加(emp);
}
公共作废打印部门员工()
{
//循环遍历列表中的所有员工,并将方法调用为string with,将返回员工的字符串。
foreach(var emp in_员工)
{
Console.WriteLine(“=====================================================================================================================================”);
//将字符串写入控制台。
Console.WriteLine(emp.ToString());
}
}
公共机构雇员总数()
{
//返回员工列表中的员工总数。
返回_employees.Count;
}
}
添加员工后的结果

顺便说一句,为什么不在这段代码中使用getter和setter呢?我不明白问题是什么。还有,为什么不使用属性?为什么所有的成员
都是公共的
(有效地使getter和setter变得毫无意义)?出了什么问题,或者你想实现什么?问题不清楚,“在main中声明类”-您的意思是在
main()
中赋值吗?如何使用属性?邪恶的羊