懂C#的进,我是新手,请各位大大帮忙看下下面的代码有什么问题,谢谢!
employee[0].Name = "Tom";
employee[0].Age = "10";
employee[0].Sex = "man";
employee[0].Wage = 2000.00;
employee[0].Positon = "开发";
employee[1].Name = "Lily";
employee[1].Age = "20";
employee[1].Sex = "woman";
employee[1].Wage = 2000.00;
employee[1].Positon = "测试";
foreach (Employee x in employee)
{
Console.WriteLine(x.DisPlayInfo());
Console.ReadKey(); 展开
首先创建一个类:
namespace employees
{
class Employee
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
private string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
private double wage;
public double Wage
{
get { return wage; }
set { wage = value; }
}
private string position;
public string Position
{
get { return position; }
set { position = value; }
}
public string DisPlayInfo()
{
return "Name:" + this.Name + " Sex:" + this.Sex + "Age:" + this.Age + "Wage" + this.Wage + "Position:" + this.Position;
}
}
}
其次,主函数:
namespace employees
{
class Program
{
static void Main(string[] args)
{
Employee[] employee = new Employee[2];
//实例化employee[0]对象
employee[0] = new Employee();
employee[0].Name = "Tom";
employee[0].Age = 10;
employee[0].Sex = "man";
employee[0].Wage = 2000.00;
employee[0].Position = "开发";
//实例化employee[1]对象
employee[1] = new Employee();
employee[1].Name = "Lily";
employee[1].Age = 20;
employee[1].Sex = "woman";
employee[1].Wage = 2000.00;
employee[1].Position = "测试";
foreach (Employee x in employee)
{
Console.WriteLine(x.DisPlayInfo());//显示
}
Console.ReadKey();
}
}
}
结果:
DisPlayInfo()是实现了的,就是显示功能,return "Name:" + this.Name + " Sex:" + this.Sex + ", Age:" + this.Age +" Wage" + this.Wage +" Positon:" + this.Positon;这是部分代码,ReadKey方法是系统方法的调用,不用实现,我只想知道我上面的代码有没什么问题?
那要看你怎么定义“问题”这个词了,如果是编译和语法错,那还好只是“缺少}和{搭配”,不过你直接在程序里面赋值真的感觉怪怪的。
言归正传,个人觉得还是把类中的各个字段类型标注清楚比较好,还有哪有都用字符串赋值的呀,Wage和Age就应该是double和int类型呀。字符串赋值是单引号怎么写成双引号了,基本功不太扎实,不过都是小问题。