创建一个Employee类。要包含为数据成员的项目是 员工编号,姓名,聘用日期,职位描述,部门和 月收入。该类通常用于显示字母 列出所有员工。包括适当的构造函数和 属性。重写ToString()方法以返回所有数据 成员。创建第二个类来测试您的Employee类。
我已经使用适当的变量,属性和构造函数创建了一个Employee类,但是我遇到了麻烦"测试"通过第二堂课。我编写的代码运行没有错误,但没有显示任何内容(可能是测试的目标)。我在调用部分哪里出错?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EmployeeProgram
{
public class EmployeeProgram
{
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 = 456;
this.name = "Joyce";
this.hiredate = "12/15/14";
this.monthlySalary = 3200;
this.description = "Manager";
this.department = "Accounting";
}
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());
}
}
}
答案 0 :(得分:-1)
根据您发布的代码,您无需在任何地方调用Console.WriteLine()。你有一个Print方法,但在调用之前它不会被运行。您需要创建该类的实例,然后将实例的ToString方法写入控制台,或者您可以在实例上调用方法Print。
主要是你可以做到
Employee someone = new Employee(1, "John", "01/01/2016", 5000, "Engineer", "R&D");
Console.WriteLine(someone.ToString());
Console.ReadKey(); // Prevent the console from closing
在您的Employee构造函数中,您应该使用它要求的参数,例如
this.employeeNumber = employeeNumber.
通常应该避免使用与成员变量/属性匹配的参数名称。
如果您在获取和设置属性中没有做任何特殊操作,则可以使用自动属性
public int EmployeeNumber { get; set; }