同事问到C#中的属性(Property)怎么理解?
同事问到C#中的属性(Property)怎么理解?我说“属性就是对私有字段进行读取。”事后分析,其实属性还是值得研究的,首先属性是类、结构、接口的命名成员,它提供了通过访问器读、写或者计算私有字段值的灵活机制。举个例子:通过User类来记录用户注册信息。
注意:属性(Property)的调用跟类的字段的调用方法一样,赋值用等号。读取跟类的字段的读取方法一样。值得注意的是:private string name; public string Name;这样在C#中没有问题,但是如果在一个工程文件中存在多种语言,比如VB.NET.那么就会出现问题,个人建议把字段名称前面加一个标志符,比如:u_name、U_sex.这样就方便很多。
1
using System;
2
class User
3
{
4
public string name;
5
public string sex;
6
}
7
class Property
8
{
9
static void Main()
10
{
11
User zm=new User();
12
zm.name="无为";
13
zm.sex="天";
14
Console.WriteLine("姓名:"+zm.name+"性别:"+zm.sex);
15
}
16
}
17
此时如果用户输入的性别为其他的,比如“猪”,哈哈,是不是一样可以通过编译器了?解决方法是:
using System; 2
class User 3
{ 4
public string name; 5
public string sex; 6
} 7
class Property 8
{ 9
static void Main() 10
{ 11
User zm=new User(); 12
zm.name="无为"; 13
zm.sex="天"; 14
Console.WriteLine("姓名:"+zm.name+"性别:"+zm.sex); 15
} 16
} 17

1
using System;
2
class User
3
{
4
public string name;
5
public string sex;
6
public void SetSex(string values)
7
{
8
if (values=="男"||values=="女")
9
{
10
sex=values;
11
}
12
else
13
{
14
Console.WriteLine("性别只能是男或者女");
15
}
16
}
17
}
18
class Property
19
{
20
static void Main()
21
{
22
User zm=new User();
23
zm.name="无为";
24
zm.SetSex("天");
25
Console.WriteLine("姓名:"+zm.name+"性别:"+zm.sex);
26
}
27
}
28
问题解决:但是值得注意的是name、sex还是public属性,依然可以修改,给name、sex设置为public,破坏了类的封装,存在潜在的危险。应该设置为private.这样就需要用到属性的知识去声明,读取这两个字段。代码如下:
using System; 2
class User 3
{ 4
public string name; 5
public string sex; 6
public void SetSex(string values) 7
{ 8
if (values=="男"||values=="女") 9
{ 10
sex=values; 11
} 12
else 13
{ 14
Console.WriteLine("性别只能是男或者女"); 15
} 16
} 17
} 18
class Property 19
{ 20
static void Main() 21
{ 22
User zm=new User(); 23
zm.name="无为"; 24
zm.SetSex("天"); 25
Console.WriteLine("姓名:"+zm.name+"性别:"+zm.sex); 26
} 27
} 28

1
using System;
2
class User
3
{
4
private string name;
5
private string sex;
6
public void SetName(string values)
7
{
8
name=values;
9
}
10
public string GetName()
11
{
12
return name;
13
}
14
public void SetSex(string values)
15
{
16
if (values=="男"||values=="女")
17
{
18
sex=values;
19
}
20
else
21
{
22
Console.WriteLine("性别只能为“男”或者“女”");
23
}
24
}
25
public string GetSex()
26
{
27
return sex;
28
}
29
public string Name
30
{
31
get
32
{
33
return name;
34
}
35
set
36
{
37
name=value;//隐含参数
38
}
39
}
40
public string Sex
41
{
42
get
43
{
44
return sex;
45
}
46
set
47
{
48
if (value=="男"||value=="女")
49
{
50
sex=value;
51
}
52
else
53
{
54
Console.WriteLine("性别只能为“男”或者“女”");
55
}
56
}
57
}
58
}
59
class Property
60
{
61
static void Main()
62
{
63
User zm=new User();
64
zm.SetName("无为");
65
zm.SetSex("男");
66
Console.WriteLine("姓名:"+zm.GetName()+" 性别:"+zm.GetSex()); //方法调用
67
zm.Name="WuWei"; //属性调用
68
zm.Sex="天";
69
Console.WriteLine("姓名:"+zm.Name+" 性别:"+zm.Sex);
70
}
71
}
72
从运行结果上看,把“无为”改成了“WuWei”,并且zm.Sex="天"没有对性别做修改。
using System; 2
class User 3
{ 4
private string name; 5
private string sex; 6
public void SetName(string values) 7
{ 8
name=values; 9
} 10
public string GetName() 11
{ 12
return name; 13
} 14
public void SetSex(string values) 15
{ 16
if (values=="男"||values=="女") 17
{ 18
sex=values; 19
} 20
else 21
{ 22
Console.WriteLine("性别只能为“男”或者“女”"); 23
} 24
} 25
public string GetSex() 26
{ 27
return sex; 28
} 29
public string Name 30
{ 31
get 32
{ 33
return name; 34
} 35
set 36
{ 37
name=value;//隐含参数 38
} 39
} 40
public string Sex 41
{ 42
get 43
{ 44
return sex; 45
} 46
set 47
{ 48
if (value=="男"||value=="女") 49
{ 50
sex=value; 51
} 52
else 53
{ 54
Console.WriteLine("性别只能为“男”或者“女”"); 55
} 56
} 57
} 58
} 59
class Property 60
{ 61
static void Main() 62
{ 63
User zm=new User(); 64
zm.SetName("无为"); 65
zm.SetSex("男"); 66
Console.WriteLine("姓名:"+zm.GetName()+" 性别:"+zm.GetSex()); //方法调用 67
zm.Name="WuWei"; //属性调用 68
zm.Sex="天"; 69
Console.WriteLine("姓名:"+zm.Name+" 性别:"+zm.Sex); 70
} 71
} 72

注意:属性(Property)的调用跟类的字段的调用方法一样,赋值用等号。读取跟类的字段的读取方法一样。值得注意的是:private string name; public string Name;这样在C#中没有问题,但是如果在一个工程文件中存在多种语言,比如VB.NET.那么就会出现问题,个人建议把字段名称前面加一个标志符,比如:u_name、U_sex.这样就方便很多。



浙公网安备 33010602011771号