为家庭作业分配了两个相同形式的问题,所以我会发布第一个:
“创建一个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");
}
}
}
答案 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();
}
}
}
但正如其他人所指出的那样,您的代码存在很多问题:
EmployeeProgram.Employee
而不是EmployeeProgram.employee
meployee
方法中应该Employee
而不是Main()
。employee
类中的样板代码。dateOfHire
应为DateTime
个对象,而不是string
。monthlySalary
等参数通常为decimal
类型,而非int
。