在另一个 class 中获取对变量的访问权限
Gaining access to a variable in another class
我 几分钟前遇到了一个问题。
为了简单起见?我有一个 "Member" class 和一个 "Race" class.
我正在尝试访问成员 class 中的 "MemType" 变量(这是一个私有属性),因为我想将它与 "RaceType"
private string MemType;
/// <summary>
/// Username holds the Members Username (JM6491SHR, MM5642SHR)
/// </summary>
private string Username;
/// <summary>
/// Number of races that the racer has run (1, 10, 134)
/// </summary>
private int NoRacesRun;
/// <summary>
/// The % of Races that the racer has won (0.0%, 50.6%, 13.8%)
/// </summary>
private float PerRacesWon;
private string MemPic;
private string MemClub;
private bool Gender;
private int MemExp;
/// <summary>
/// Need a get property for MemType to compare it to a Race type (used for validation)
/// </summary>
public string memType { get { return MemType; } }
(变量的原因是,如果 Race 和 Member 不都是 "Senior" 例如,那么成员不能参加比赛,因为他们不符合类型要求)
我使用了getter(没有setter)来获取变量。
这是正确的做法还是错误的做法?为我的会员提供了我的一些代码,显示了我使用的 属性。
是的 属性 加上 getter 就可以了。相反,您可以像这样
使用带有私有 setter 的自动实现的 属性
public string MemType { get; private set }
请记住,根据 Microsoft 命名约定 - 私有成员需要是 _memType,public 属性应以大写字母开头,例如 - MemType。在你的例子中不是相反。
这是正确的方法。您也可以将您的变量声明为静态的,但在这种情况下这是一个不好的做法。有关更多信息,您应该查看面向对象编程中的封装。
我认为您的尝试如下:
public class Example
{
private string demo;
public class Example(string demo)
{
this.demo = demo;
}
public string Demo
{
get { return this.demo; }
private set { this.demo = value; }
}
}
现在在完全独立的 class 中,您将:
public class ExampleCont
{
private Example example;
public ExampleCont(Example example)
{
this.example = example;
}
}
现在,在整个 ExampleCont
期间,您都可以通过 example.Demo
访问您的 属性。希望对你有帮助。
我 几分钟前遇到了一个问题。
为了简单起见?我有一个 "Member" class 和一个 "Race" class.
我正在尝试访问成员 class 中的 "MemType" 变量(这是一个私有属性),因为我想将它与 "RaceType"
private string MemType;
/// <summary>
/// Username holds the Members Username (JM6491SHR, MM5642SHR)
/// </summary>
private string Username;
/// <summary>
/// Number of races that the racer has run (1, 10, 134)
/// </summary>
private int NoRacesRun;
/// <summary>
/// The % of Races that the racer has won (0.0%, 50.6%, 13.8%)
/// </summary>
private float PerRacesWon;
private string MemPic;
private string MemClub;
private bool Gender;
private int MemExp;
/// <summary>
/// Need a get property for MemType to compare it to a Race type (used for validation)
/// </summary>
public string memType { get { return MemType; } }
(变量的原因是,如果 Race 和 Member 不都是 "Senior" 例如,那么成员不能参加比赛,因为他们不符合类型要求)
我使用了getter(没有setter)来获取变量。
这是正确的做法还是错误的做法?为我的会员提供了我的一些代码,显示了我使用的 属性。
是的 属性 加上 getter 就可以了。相反,您可以像这样
使用带有私有 setter 的自动实现的 属性public string MemType { get; private set }
请记住,根据 Microsoft 命名约定 - 私有成员需要是 _memType,public 属性应以大写字母开头,例如 - MemType。在你的例子中不是相反。
这是正确的方法。您也可以将您的变量声明为静态的,但在这种情况下这是一个不好的做法。有关更多信息,您应该查看面向对象编程中的封装。
我认为您的尝试如下:
public class Example
{
private string demo;
public class Example(string demo)
{
this.demo = demo;
}
public string Demo
{
get { return this.demo; }
private set { this.demo = value; }
}
}
现在在完全独立的 class 中,您将:
public class ExampleCont
{
private Example example;
public ExampleCont(Example example)
{
this.example = example;
}
}
现在,在整个 ExampleCont
期间,您都可以通过 example.Demo
访问您的 属性。希望对你有帮助。