在C#中调用Main的其他类

时间:2015-02-22 19:12:15

标签: c#

为家庭作业分配了两个相同形式的问题,所以我会发布第一个:

“创建一个Employee类。要包含为数据成员的项目是 员工编号,姓名,聘用日期,职位描述,部门和 月收入。该类通常用于显示所有员工的按字母顺序排列的列表。包括适当的构造函数和属性。覆盖 ToString()方法返回所有数据成员。创建第二个类 测试你的Employee类。“

我已经使用适当的变量,属性和构造函数创建了一个Employee类,但是在通过第二个类“测试”它时遇到了麻烦。我编写的代码运行没有错误,但没有显示任何内容(可能是测试的目标)。我在调用部分哪里出错?

员工信息部分:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EmployeeProgram
{
    public class employee
    {
        private int employeeNumber;
        private string name;
        private string hiredate;
        private int monthlySalary;
        private string description;
        private string department;

    public employee(int employeeNumber, string name, string dateOfHire, int monthlySalary, string description, string department)
    {

        this.employeeNumber = 321;
        this.name = "Alex";
        this.hiredate = "01/02/15";
        this.monthlySalary = 2500;
        this.description = "Corporate grunt";
        this.department = "Sales";
    }

    public int EmployeeNumber
    {
        get
        {
            return employeeNumber;
        }
        set
        {
            employeeNumber = value;
        }
    }

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }

    public string Hiredate
    {
        get
        {
            return hiredate;
        }
        set
        {
            hiredate = value;
        }
    }

    public int MonthlySalary
    {
        get
        {
            return monthlySalary;
        }
        set
        {
            monthlySalary = value;
        }
    }
    public string Department
    {
        get
        {
            return department;
        }
        set
        {
            department = value;
        }
    }
    public string Description
    {
        get
        {
            return description;
        }
        set
        {
            description = value;
        }
    }

    public override string ToString()
    {
        return "Employee ID: " + employeeNumber +
               "Employee Name: " + name +
               "Employee Hire Date: " + hiredate +
               "Employee Monthly Salary: " + monthlySalary +
               "Employee Description: " + description +
               "Employee Department: " + department;
    }

    public void Print()
    {
        Console.WriteLine(this.ToString());
    }
}

“通话部分”

namespace employee
{
    public class employeeApp
    {
        public static void Main()
        {
            EmployeeProgram.employee Employee = new EmployeeProgram.employee(321, "Alex", "1/02/15", 2500, "Corporate grunt", "Sales");

        }

    }
}

1 个答案:

答案 0 :(得分:0)

您需要在Print()方法中调用EmployeeProgram.employee main()方法。

namespace employee
{
    public class employeeApp
    {
        public static void Main()
        {
            EmployeeProgram.employee Employee = new EmployeeProgram.employee(321, "Alex", "1/02/15", 2500, "Corporate grunt", "Sales");
            Employee.Print();
        }

    }
}

但正如其他人所指出的那样,您的代码存在很多问题:

  • 类名应该是Pascal大小写,即以大写字母开头。因此,它应该是EmployeeProgram.Employee而不是EmployeeProgram.employee
  • 私有变量通常遵循Camel大小写,即以小写字母开头。因此,meployee方法中应该Employee而不是Main()
  • 您可以通过使用自动属性来减少employee类中的样板代码。
  • 参数dateOfHire应为DateTime个对象,而不是string
  • monthlySalary等参数通常为decimal类型,而非int