通过以下代码终于明白属性与字段,方法怎么区会。
using System;
using System.Collections.Generic;using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ForBookTest
{
class Employee
{
private string empName;
private int empID;
private float currPay;
private int empAge;
private string empSSN;
private static string companyName;
public string SocialSecurityNumber
{
get { return empSSN; }
set { empSSN = value; }
}
public static string Company
{
get { return companyName; }
set { companyName = value; }
}
public int ID
{
get { return empID; }
set { empID = value; }
}
public float Pay
{
get { return currPay; }
set { currPay = value; }
}
public string Name
{
get { return empName; }
set {
if (value.Length>15)
Console.WriteLine("Error! Name must be less than 16 characters!");
else
empName = value;
}
}
public int Age
{
get { return empAge; }
set { empAge = value; }
}
public void GiveBonus(float amount)
{
Pay += amount;
}
public void DisplayStats()
{
Console.WriteLine("Name:{0}", empName);
Console.WriteLine("ID:{0}", empID);
Console.WriteLine("Pay:{0}", Pay);
}
public Employee() { }
public Employee(string name, int id, float pay) : this(name, 0, id, pay, "") { }
public Employee(string name, int id, float pay)
{
empName = name;
empID = id;
currPay = pay;
}
public Employee(string name, int age, int id,float pay,string ssn)
{
Name = name;
Age = age;
ID = id;
Pay = pay;
SocialSecurityNumber = ssn;
}
}
}