在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ C#/ C#屬性(Properties)
C#屬性(Properties)
C#與Java比較
C#方法
C#枚舉
C#關鍵字
C# StreamReader類
C#不安全代碼
C#文件(I/O)
C#匿名方法
C#線程同步
C# Thread類
C#主線程
C#數據類型
C# FileStream類
C#預處理指令
C#繼承
C#循環(huán)
C#決策結構
C#集合
C#反射
C#類型轉換
C#泛型
C# StringReader類
C#歷史
C#運算符重載
C#屬性
C#線程實例:Sleep()方法
C#線程示例:優(yōu)先級
C#線程實例:Join()方法
C# BinaryReader類
C#類
C#索引器
C# BinaryWriter類
C#序列化
C#常量和文字
C#程序結構
C#封裝
C#事件
C#可空類型(nullable)
C#基本語法
C#異常處理
C#教程
C#接口
C# System.IO命名空間
C#線程命名實例
C# StringWriter類
C#線程實例
C#數組
C#正則表達式
C#命名空間
C#反序列化
C#與C++比較
C# TextWriter類
C#多線程
C#字符串
C#是什么?
C#變量
C# FileInfo類
C#線程實例:Abort()方法
C#結構體
C#運算符
C#入門程序
C#多線程生命周期
C# TextReader類
C# DirectoryInfo類
C#委托

C#屬性(Properties)

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

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

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

訪問器

屬性的訪問器包含有助于獲取(讀取或計算)或設置(寫入)屬性的可執(zhí)行語句。訪問器聲明可以包含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();
      }
   }
}

當上述代碼被編譯并執(zhí)行時,它產生以下結果:

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();
        }
    }
}

當上述代碼被編譯并執(zhí)行時,它產生以下結果:

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

上一篇:C#事件下一篇:C#命名空間