调用另一个类的问题

时间:2013-11-15 03:44:54

标签: c# class

第一课如下:

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名称在当前上下文中不存在  *在我的employeeApp类中,与dateOfHire相关"不能隐式地将int转换为字符串

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

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

问题1 - C#区分大小写。资本化主要。不需要使用public访问修饰符,通常不建议Main使用。

static void Main()

问题2 - 对于第二个name = Cody;,我猜你的意思是...... name = "Cody";

问题3 - 对于第三个问题,您需要通过在int值上调用ToString()将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();
    }

答案 1 :(得分:1)

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

static void Main()

2.您必须在班级employe中创建构造函数 3.您必须将String分配给String变量,但是您要分配日期。 如下:

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;
    }


}
}