-1

第一类如下:

public class employeeApp
{
    public static void main()
    {
        EmployeeProgram.employee Employee = new EmployeeProgram.employee( );
    }

    public void employee(int employeeNumber, string name, string dateOfHire, int monthlySalary)
    {
        employeeNumber = 123;
        name = Cody;
        dateOfHire = 01/01/11;
        monthlySalary = 2500;
    }
}

第二类如下:

/*
 * Mosbrucker_C_PRO_01              Author: Mosbrucker, Cody
 * Creates a class for employee with data members;
 * Employee number, name, date of hire, and monthly salary.
 * ****************************************************/

public class employee
{
      private int employeeNumber;
      private string name;
      private string dateOfHire;
      private int monthlySalary;


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

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

    public string DateOfHire
    {
        get
        {
            return dateOfHire;
        }
        set
        {
            dateOfHire = value;
        }
    }

    public int MonthlySalary
    {
        get
        {
            return monthlySalary;
        }
        set
        {
            monthlySalary = value;
        }
    }

    public override string ToString()
    {
        return "Employee Id: " + employeeNumber +
               "Employee Name: " + name +
               "Employee Date of Hire: " + dateOfHire +
               "Employee Monthly Salary: " + monthlySalary;
    }
}

我遇到的问题是:*在我的employeeApp 类中“不包含适合入口点的“静态”主方法”*在我的employeeApp 类中“名称Cody 在当前上下文中不存在*在我与dateOfHire 相关的employeeApp 类中“不能将 int 隐式转换为字符串

我正在为一个班级和它的任务做这个:创建一个员工班级。作为数据成员包括的项目是员工编号、姓名、雇用日期和月薪。包括适当的构造函数和属性。重写 ToString ( ) 方法以返回所有数据成员。创建第二个类来测试您的 Employee 类。

非常感谢任何帮助。

4

2 回答 2

1

问题 1 - C# 区分大小写。大写主要。访问修饰符的使用public不是必需的,通常不建议用于Main.

static void Main()

第 2 期 -第二个name = Cody;我猜你的意思是......name = "Cody";

问题 3 -对于第三个问题,您需要通过调用ToString()int 值将 int 值转换为字符串。 employeeNumber.ToString()monthlySalary.ToString()

这里有很多问题,而且都是相当基本的。我建议您使用谷歌或解释为什么您无法解决它们。否则,您可能没有付出必要的努力来自己解决问题。

问题 4至于 I/O 写入问题,this由于局部变量和私有字段之间的命名冲突,您需要使用关键字进行限定:

public class employee
{
    private int employeeNumber;
    private string name;
    private string dateOfHire;
    private int monthlySalary;

    public employee(int employeeNumber, string name, string dateOfHire, int monthlySalary)
    {
        this.employeeNumber = 123;//because you have naming collissions you need to use `this`
        this.name = "Cody";
        this.dateOfHire = "01 / 01 / 11";
        this.monthlySalary = 2500;
    }

    public override string ToString()
    {
        return "Employee Id: " + employeeNumber +
               "Employee Name: " + name +
               "Employee Date of Hire: " + dateOfHire +
               "Employee Monthly Salary: " + monthlySalary;
    }


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

然后主要

    static void Main(string[] args)
    {
        employee e = new employee(1,"","",0);//these values are ignored the way you set this up
        e.Print();
        Console.ReadLine();
    }
于 2013-11-15T03:47:45.397 回答
1

1.在 C# 中,我们使用 Main(Capital M) 所以 Main 方法应该是:

static void Main()

2.您必须在您的类中创建构造函数employe
3.您必须将字符串分配给字符串变量,但您正在分配日期。如下 :

dateOfHire = 01/01/11;

在你的构造函数中

4.Cody应该"Cody"在你的构造函数中表示为字符串
5.在为类中的局部变量分配数据时,this在分配具有相同名称的变量时用于表示当前对象

示例:this.employeenumber=employeenumber;
文件 1:

namespace employee
{
public class employeeApp
{
    public static void Main()
    {
        EmployeeProgram.employee Employee = new EmployeeProgram.employee(123,"Cody","11/11/11",24567);//call your constructor

    }
  }
}

文件 2:

/*
 * Mosbrucker_C_PRO_01              Author: Mosbrucker, Cody
 * Creates a class for employee with data members;
 * Employee number, name, date of hire, and monthly salary.
 * ****************************************************/
 namespace EmployeeProgram
 {
   public class employee
    {
      private int employeeNumber;
      private string name;
      private string dateOfHire;
      private int monthlySalary;

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

        this.employeeNumber = 123;
        this.name = "Cody";
        this.dateOfHire = "01/01/11";
        this.monthlySalary = 2500;
    }

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

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

    public string DateOfHire
    {
        get
        {
            return dateOfHire;
        }
        set
        {
            dateOfHire = value;
        }
    }

    public int MonthlySalary
    {
        get
        {
            return monthlySalary;
        }
        set
        {
            monthlySalary = value;
        }
    }

    public override string ToString()
    {
        return "Employee Id: " + employeeNumber +
               "Employee Name: " + name +
               "Employee Date of Hire: " + dateOfHire +
               "Employee Monthly Salary: " + monthlySalary;
    }


}
}
于 2013-11-15T03:48:38.727 回答