C#屬性(Properties)

屬性(Properties)被命名爲類,結構和接口的成員。類或結構中的成員變量或方法稱爲字段。 屬性是字段的擴展,並使用相同的語法訪問。它們使用訪問器,通過這些訪問器可以讀取,寫入或操作私有字段的值。

屬性不指定存儲位置。它們有讀取,寫入或計算其值的訪問器。

例如,假設有一個名稱爲Student的類,其中包含年齡(age),名稱(name)和代碼(code)的私有字段。我們無法從類範圍外直接訪問這些字段,但是可以擁有訪問這些私有字段的屬性。

訪問器

屬性的訪問器包含有助於獲取(讀取或計算)或設置(寫入)屬性的可執行語句。訪問器聲明可以包含get訪問器和set訪問器。例如:

// Declare a Code property of type string:
public string Code
{
   get
   {
      return code;
   }
   set
   {
      code = value;
   }
}

// Declare a Name property of type string:
public string Name
{
   get
   {
      return name;
   }
   set
   {
      name = value;
   }
}

// Declare a Age property of type int:
public int Age
{ 
   get
   {
      return age;
   }
   set
   {
      age = value;
   }
}

例子

以下示例演示瞭如何使用屬性:

using System;
namespace yiibai
{
   class Student
   {
      private string code = "N.A";
      private string name = "not known";
      private int age = 0;

      // Declare a Code property of type string:
      public string Code
      {
         get
         {
            return code;
         }
         set
         {
            code = value;
         }
      }

      // Declare a Name property of type string:
      public string Name
      {
         get
         {
            return name;
         }
         set
         {
            name = value;
         }
      }

      // Declare a Age property of type int:
      public int Age
      {
         get
         {
            return age;
         }
         set
         {
            age = value;
         }
      }
      public override string ToString()
      {
         return "Code = " + Code +", Name = " + Name + ", Age = " + Age;
      }
   }

   class ExampleDemo
   {
      public static void Main()
      {

         // Create a new Student object:
         Student s = new Student();

         // Setting code, name and the age of the student
         s.Code = "10010";
         s.Name = "Maxsu";
         s.Age = 24;
         Console.WriteLine("Student Info: {0}", s);

         //let us increase age
         s.Age += 1;
         Console.WriteLine("Student Info: {0}", s);
         Console.ReadKey();
      }
   }
}

當上述代碼被編譯並執行時,它產生以下結果:

Student Info: Code = 10010, Name = Maxsu, Age = 24
Student Info: Code = 10010, Name = Maxsu, Age = 25

抽象屬性

抽象類可能有一個抽象屬性,它應該在派生類中實現。以下程序說明了這一點:

using System;
namespace yiibai
{
    public abstract class Person
    {
        public abstract string Name
        {
            get;
            set;
        }
        public abstract int Age
        {
            get;
            set;
        }
    }

    class Student : Person
    {

        private string code = "N.A";
        private string name = "N.A";
        private int age = 0;

        // Declare a Code property of type string:
        public string Code
        {
            get
            {
                return code;
            }
            set
            {
                code = value;
            }
        }

        // Declare a Name property of type string:
        public override string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        // Declare a Age property of type int:
        public override int Age
        {
            get
            {
                return age;
            }
            set
            {
                age = value;
            }
        }
        public override string ToString()
        {
            return "Code = " + Code + ", Name = " + Name + ", Age = " + Age;
        }
    }

    class ExampleDemo
    {
        public static void Main()
        {
            // Create a new Student object:
            Student s = new Student();

            // Setting code, name and the age of the student
            s.Code = "1011";
            s.Name = "Maxsu";
            s.Age = 21;
            Console.WriteLine("Student Info:- {0}", s);

            //let us increase age
            s.Age += 1;
            Console.WriteLine("Student Info:- {0}", s);
            Console.ReadKey();
        }
    }
}

當上述代碼被編譯並執行時,它產生以下結果:

Student Info:- Code = 1011, Name = Maxsu, Age = 21
Student Info:- Code = 1011, Name = Maxsu, Age = 22