字段、属性、索引器


1 字段

         字段(field)有些也翻译为域,是声明类或结构中的变量,用来表示类的状态(或属性);其类型可以是基本数据类型,也可以是自定义数据类型;可以是静态(类)成员,也可以是非静态成员(为对象所有)。

         字段是用来表示类的状态(或属性),即静态行为,其定义是为了满足类对对象抽象的需要,暗含面向对象的思想,即把属性封装,数据与操作分离。

         比如,一个学生有学号、姓名,则可以如下定义字段(以下不纠结于实现):

		private string stu_nu;
		private string stu_na;

2 属性

         前面说到字段,而属性则是提供对字段的值进行读取、改写或计算的一组特殊方法,该组方法称作访问器,其优点是访问灵活、使用方便;其分为get 、set 访问器,且各自可以被不同的修饰符修饰,以提供不同的效果。

         对前面的字段设置属性如下:

		public string Stu_nu
        {
            get { return stu_nu; }
            set { stu_nu = value; }
        }
		public string Str_na
        {
            get { return stu_na; }
            set { 
                if(value != null)
                stu_na = value; }
        }

         使用属性能够实现代码的封装,从而使得程序变得较为安全。其中 set 方法使用 value 代替所赋的值,两种方法不一定都需要实现,相应的功能也就不相同。在 VS2010 (或更高)版本中可以使用快捷方式自动生成。

3 索引器

         索引器同属性一样,都是为了便于访问字段,而索引器可以方便的将类、结构或接口当做数组来使用,其主要用于封装内部集合或数组的类型中;由于索引器的值不少变量,所以不可用做ref 或 out 参数进行传递;其不同于属性在于其具有参数列表(签名)。

         对前面的字段设置索引器如下:

		public string this[int index]
        {
            get {
                if (index == 0)
                    return stu_nu;
                else if (index == 1)
                    return stu_na;
                else return null;
            }
            set {
                if (index == 0)
                    stu_nu = value;
                else if (index == 1)
                    stu_na = value;
            }
        }

         在使用索引器时,需要使用this 关键字。

4 实例与实现

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 索引器test
{
    public class Student
    {
        //定义私有成员
        private string stu_nu;        
        private string stu_na;

        //定义字段
        public string Stu_nu
        {
            get { return stu_nu; }
            set { stu_nu = value; }
        }
        public string Str_na
        {
            get { return stu_na; }
            set { 
                if(value != null)
                stu_na = value; }
        }

        //定义索引器
        public string this[int index]
        {
            get {
                if (index == 0)
                    return stu_nu;
                else if (index == 1)
                    return stu_na;
                else return null;
            }
            set {
                if (index == 0)
                    stu_nu = value;
                else if (index == 1)
                    stu_na = value;
            }
        }

    }
    
    
    class ProgramTestStudent
    {
        //测试学生类
        static void Main(string[] args)
        {
            Student s1 = new Student();
            s1[0] = "2101777";
            s1[1] = "ch";
            Console.WriteLine("use index show");
            Console.WriteLine("student number is {0}, his name is {1}", s1[0], s1[1]);
            Console.WriteLine("use property show");
            Console.WriteLine("student number is {0}, his name is {1}", s1.Stu_nu, s1.Str_na);
        }
    }
}

5 参考

[1]Msdn C# 编程指南

[2] 杨睿娜C#索引器的使用方法



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值