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