C#零基础学习之路——属性
一. 学习背景
基于学习b站刘老师的c#入门,整理学习心得。
二. 属性
2.1 什么是属性
属性简要理解为四个要点:
1.属性是类的一种成员;
2.属性是对字段的自然扩展;
3.属性是由get/set方法对进化而来;
4.属性是"语法糖"
2.2 属性从字段的演变
首先打开Visual Studio 2017,新建一个C#控制台应用程序,将项目取名为LearningProperty,之后在项目后台键入如下代码1:
using System;
using System.Collections.Generic;
using System.Linq;
namespace LearningProperty
{
class Program
{
static void Main(string[] args)
{
Student stu1 = new Student();
stu1.age = 20;
Student stu2 = new Student();
stu2.age = 200;
Student stu3 = new Student();
stu3.age = 30;
//创建学生的集合
List<Student> stuList = new List<Student>();
stuList.Add(stu1);
stuList.Add(stu2);
stuList.Add(stu3);
//学生们的年龄总和
int totalAge = 0;
foreach (var stu in stuList)
{
totalAge += stu.age;
}
//学生们的平均年龄
int avgAge = totalAge / stuList.Count();
Console.WriteLine(avgAge);
Console.ReadKey();
}
}
//创建学生类
public class Student
{
public int age;
}
}
代码说明:这里首先创建一个Student类,给Student类定义一个名为age的字段,创建3个Student类的实例,并给其字段进行赋值。对学生的年龄计算总和并求其平均年龄。仔细点会发现,我们设置其中的一个学生年龄为200,这是不符合人类寿命的存在,这也是为后面的属性做准备,我们想在设置学生年龄的时候,去判断年龄是否正常,避免像现在这样不小心的误键,下面将一步步的走进属性的世界。在学生类中添加两个方法,代码2如下:
using System;
using System.Collections.Generic;
using System.Linq;
namespace LearningProperty
{
class Program
{
static void Main(string[] args)
{
Student stu1 = new Student();
stu1.SetAge(20);
Student stu2 = new Student();
stu2.SetAge(200);
Student stu3 = new Student();
stu3.SetAge(30);
//创建学生的集合
List<Student> stuList = new List<Student>();
stuList.Add(stu1);
stuList.Add(stu2);
stuList.Add(stu3);
//学生们的年龄总和
int totalAge = 0;
foreach (var stu in stuList)
{
totalAge += stu.GetAge();
}
//学生们的平均年龄
int avgAge = totalAge / stuList.Count();
Console.WriteLine(avgAge);
Console.ReadKey();
}
}
//创建学生类
public class Student
{
public int age;
public int GetAge() { return age; }
public void SetAge(int value)
{
if (value > 0 && value < 120)
{
age = value;
}
else
{
throw new Exception("this is a worry age");
}
}
}
}
代码说明:添加GetAge()和SetAge()方法后,当我们创建学生实例后,学生的年龄受到了限制,我们调用SetAge()去设置学生的年龄,GetAge()获取学生的年龄,避免了学生年龄可以随意设置。如果超出代码中正常的范围就会抛出异常。
2.3 属性的引出
在前面说过属性是由get/set方法对进化而来,属性是对字段的自然扩展,说白了属性就是将代码2中的GetAge()和SetAge()方法进行了包装,下面的代码演示了属性对字段和两种方法封装。代码3如下:
using System;
using System.Collections.Generic;
using System.Linq;
namespace LearningProperty
{
class Program
{
static void Main(string[] args)
{
Student stu1 = new Student();
stu1.Age = 20;
Student stu2 = new Student();
stu2.Age = 200;
Student stu3 = new Student();
stu3.Age = 20;
//创建学生的集合
List<Student> stuList = new List<Student>();
stuList.Add(stu1);
stuList.Add(stu2);
stuList.Add(stu3);
//学生们的年龄总和
int totalAge = 0;
foreach (var stu in stuList)
{
totalAge += stu.Age;
}
//学生们的平均年龄
int avgAge = totalAge / stuList.Count();
Console.WriteLine(avgAge);
Console.ReadKey();
}
}
//创建学生类
public class Student
{
public int age;
public int Age
{
get { return age; }
set
{
if (value > 0 && value < 120)
{
age = value;
}
else
{
throw new Exception("this is a worry age");
}
}
}
}
}
代码说明:将上面的两种方法删除后,在学生类的代码中可以发现属性的定义就是对字段进行了封装,属性的关键字修饰和字段关键字修饰是完全一样的,修改完后,我们可以像使用字段一样使用属性。
2.4 属性的秘密
属性是一种C#的语法糖,打开项目文件在文件夹中的位置,找到bin目录下的可执行文件(.exe),然后在开始菜单中搜索Developer Command Prompt for VS 2022,版本不同的找相应版本的。
打开后键入ildasm命令,敲回车后,打开反编译器后可以看见程序的结构,可以看到Age属性是微软的一种"语法糖"。
三 总结
自我总结:属性其实就是对字段的包装,同时属性也包装了get/set方法对。同时c#也可以使用快捷键来声明属性,键入prop或propfull后连按两次tab键。