2016-04-26 63 views
-4

创建一个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()); 
     } 
    } 
} 
+1

这不是C++,请不要标记无关的编程语言。此外,不知道任何C#,这看起来像它需要[mcve],而不是这个代码块。 –

+0

向我们展示您的主程序 – JSON

回答

-1

从您发布的代码中,不要在任何地方调用Console.WriteLine()。你有一个打印方法,但它不会运行,直到它被调用。您需要创建该类的实例,然后将实例的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 

在你的员工的构造函数,你应该使用它要求的参数,这样

this.employeeNumber = employeeNumber. 

通常应该避免匹配构件,其参数名称变量/属性。

当你没有做任何事情在你的get和set性质特殊的,可以使用自动属性

public int EmployeeNumber { get; set; } 
+0

非常感谢 –