日新阁

取日三省之意,记生活珠玑,每日清新,谓日新阁.

 

c# 属性 

属性:get { //读属性代码 } set { //写属性代码 }
public class Person
{
private string name;
public string Name
{
   get{return  name;}
   set{ name=value;}
}
}
属性可以忽略get或set访问器,但是不能两个都忽略.
set访问器包含一个隐藏的参数value,该参数包含从客户代码传送过来的值.
公共属性及其底层类型最好使用相同的名称,因为它们之间的联系将很清晰.

字段使用camelCase(xxXxx),如dateOfBirth,而属性使用PacalCase(XxXxx),如DateOfBirth.一些开发人员喜欢在字段的开头使用下划线,如_Name,属性也应使用名词来命名.

c#通过属性特性读取和写入字段,而不直接读取和写入,以此来提供对类中字段的保护.

属性按可以访问的类型分为三种不同的类型:

一.读/写属性

    读/写属性是一个具有get()和set()访问器的属性.

  语法:  [访问修饰符] 数据类型 属性 
{
get{ };
set{ };
}

二.只读属性

    仅具有get()访问器属性称为只读属性.

语法: [访问修饰符] 数据类型 属性名

get{ };
}

三.只写属性

   仅具有set()访问器属性称为只写属性,不推荐使用只写属性.

语法: [访问修饰符] 数据类型 属性名
{
 set{ };
}
示例:

using System;

namespace Example1
{
 class Student
 {
  #region/***属性***/
  /// <summary>
  /// 姓名
  /// </summary>
  private string name;
  public string Name
  {
   get
   {
    return name;
   }

   set
   {
    if(value.length<40)
    {
     Console.WriteLine("学生姓名长度不能小于4个!");
     return;
    }
    name=value;
   }
  }
  #region

  static void Main(string[ ] args)
  {
   Student student=new Student();
   student.Name=Console.ReadLine();
  }
 }
}

 属性(property)
-充分体现了对象的封装性:不直接操作类的数据内容,而是通过访问器进行访问,即借助于get和set对属性的值进行读写;另一方面还可以对数据的访问属性进行控制(当然也可以通过对普通域加readonly关键字来实现。
-设计原则:属性封装了对域的操作。把要访问的域设为private,通过属性中的get和set操作对域进行设置或访问。
-不能把属性作为引用类型或输出参数来进行传递。
-get方法没有参数;set方法有一个隐含的参数value。除了使用了abstract修饰符的抽象属性,每个访问器的执行体中只有分号“;”外,其他的所有属性的get访问器都通过return来读取属性的值,set访问器都通过value来设置属性的值。
-采用间接方式来访问对象的属性(间接调用get、set方法):对象.属性 = 值(调用set),变量 = 对象.属性(调用get)。
-在属性的访问声明中:
只有set访问器,表明该属性是只写的。
只有get访问器,表明该属性是只读的。
既有set访问器,又有get访问器,表明该属性是可读可写的。
private string s_filename;
       public string Filename
       {
              get
              {
                     return s_filename;
    }//get
    set
    {
          if(s_filename!=value)
          {
                s_filename = value;
     }//if
    }//set
   }//Filename
  }

l      属性和字段的比较:

Ø       属性不能使用ref/out 型参数

Ø       属性使用前必须赋值

属性使用前必须赋值,例如:

Time lunch;

lunch.Hour = 12;//错误,lunch没有初始化

属性vs.函数

l       相似点

Ø       都包含执行代码

Ø       都可以有访问修饰符

Ø       都可以有virtual, abstract, override 修饰符

Ø       都可以用在接口中

l       不同点

Ø       属性只能拥有get/set 语句

Ø       属性不可以是void 型

Ø       属性不能使用参数

Ø       属性不能使用[ ] 参数

Ø       属性不能使用括号
public int Hour

{  

...

set
{

if (value < 0 || value > 24)

throw new ArgumentException("value");

hour = value;

}



类的属性称为智能字段,类的索引器称为智能数组。由于类本身作数组使用,所以用
this作索引器的名称,索引器有索引参数值。例:
using System;
using System.Collections;

class MyListBox
{
  protected ArrayList data = new ArrayList();
  public object this[int idx]  //this作索引器名称,idx是索引参数
  {
    get
    {
      if (idx > -1 && idx < data.Count)
      {
        return data[idx];
      }
      else
      {
        return null;
      }
    }
    set
    {
      if (idx > -1 && idx < data.Count)
      {
        data[idx] = value;
      }
      else if (idx = data.Count)
      {
        data.Add(value);
      }
      else
      {
        //抛出一个异常
      }
    }
  }
}


}

尽可能编写出运行效率更高,更健壮,更容易维护的C#代码。

尽可能的使用属性(property),而不是数据成员(field)。

private   int   property1  
  public     int   Property1  
    {    
        get  
        {  
          return   property1   ;  
          }  
        set  
          {  
            if   (value>1)     //这里校验  
                property1=   value   ;  
                else    
                property1=1;  
          }
 }


 //实例属性,可读可写
  public int StrCount
  {
   get
   {
    return m_strCount;
   }
   set
   {
    if (value>m_strCount)
    {
     strArray = new string[value];
     for (int i=0;i<value;i++)
     {
      strArray[i] = String.Format("String No.{0}",i);
     }
     m_strCount = value;
    }
   }
  }

  private static string m_strName = "MyClass";
  //一个静态属性,只读
  public static string ClassName
  {
   get
   {
    return m_strName;
   }
  }


class B
{
private A _a;
public A item
{
  get
  {
    if(_a=null)
      _a=new A();
    return _a;
  }
  set{_a=value;}
}
}

posted on 2007-03-20 15:43  flashicp  阅读(43132)  评论(2编辑  收藏  举报

导航