好的,所以我根据 C# 编程书籍的练习(我想在这里学习)编写了这个程序,它要求“覆盖 ToString() 方法以返回所有数据成员”。
我做对了吗?还是我刚刚成功编写了可以编译但什么也不做的代码。ToString的目的是什么?
我花了大约 30 分钟查看其他关于此的帖子并没有弄清楚,所以我决定做这个。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication297
{
class Program
{
static void Main(string[] args)
{
String name = "Stormtrooper";
Employee s = new Employee(name);
Console.WriteLine("The type of hire is a {0}", s.Name);
Console.WriteLine("The identification number is {0}", s.Number);
Console.WriteLine("The date of hire is {0} ABY", s.Date);
Console.WriteLine("The standard galactic salary is...{0:C}", s.Salary);
}
class Employee
{
private string _name;
private string _number;
private int _date;
private int _salary;
public string Name
{
get
{
return _name;
}
}
public string Number
{
get
{
return _number;
}
}
public int Date
{
get
{
return _date;
}
}
public int Salary
{
get
{
return _salary;
}
}
public Employee(string n)
{
_name = n;
_number = "AA23TK421";
_date = 4;
_salary = 800;
}
}
public override string ToString()
{
return "_name + _number + _date + _salary".ToString();
}
}
}