C#中的get和set访问器可用来获取和设置类中字段(即属性)的值,通过get和set访问器提供访问接口,从而可以避免对字段的直接访问造成的不安全性。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
User user = new User(12115789, "兄弟", "余华", 78.5);
user.PrintInfo();
Console.ReadLine();
}
}
class User
{
/* ---------------方法1:开始--------------- */
private int id; //书本ID
private string name; //书本名称
private string author; //书本作者
private double price; //书本价格
public int Id
{
get
{
return id;
}
set
{
id = value;
}
}
public string Name
{
get
{
return n