C#上下文中的各种属性
示例
public class Person
{
//id属性可以由其他类读取,但只能由Person类设置
public int Id {get; private set;}
//可以检索或分配Name属性
public string Name {get; set;}
private DateTime dob;
//出生日期属性存储在私有变量中,但可以通过公共属性检索或分配。
public DateTime DOB
{
get { return this.dob; }
set {this.dob= value; }
}
//Age属性只能被检索;它的值是从出生日期算起的
public int Age
{
get
{
int offset = HasHadBirthdayThisYear() ? 0 : -1;
return DateTime.UtcNow.Year - this.dob.Year + offset;
}
}
//这不是属性而是方法;尽管可以根据需要将其重写为属性。
private bool HasHadBirthdayThisYear()
{
bool hasHadBirthdayThisYear = true;
DateTime today = DateTime.UtcNow;
if (today.Month > this.dob.Month)
{
hasHadBirthdayThisYear = true;
}
else
{
if (today.Month == this.dob.Month)
{
hasHadBirthdayThisYear =today.Day> this.dob.Day;
}
else
{
hasHadBirthdayThisYear = false;
}
}
return hasHadBirthdayThisYear;
}
}
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下