我正在创建一个简单的 User 类,如果我使用带有私有字段的公共属性而不是仅使用公共字段,这有关系吗?
这是我的意思的一个例子:
public class clsUser
{
private string name;
private string lName;
public string Name
{
get
{
return name;
}
set
{
name= value;
}
}
public string LName
{
get
{
return lName;
}
set
{
lName= value;
}
}
public clsUser(string userID)
{
//get the user id here and set the properties
this.name= getName(userID);
this.lName= getLName(userID);
}
}
还是我可以做
public string name;
public string lName;
public
现在担心输入所有这些:
public string Name
{
get
{
return name;
}
set
{
name= value;
}
}
然后我将在另一个页面上使用这些填充一个表单,如下所示:
clsUser cUser - new clsUser("myid");
txtSomething.Text = cUser.name;
等等...
我想我的问题是为什么我需要先将属性重新输入为私有然后再输入为公共(正如我在所有网络示例中看到的那样)。为什么不一开始就把它们公之于众呢?