سه شنبه ۲۷ شهريور ۱۴۰۳
Tut24 آموزش برنامه نویسی و مجله تخصصی فناوری ورود/عضویت

آموزش سی شارپ - خاصیت ها

خواص (Properties) عضوهای نامگذاری شده‌ای هستند که در کلاس‌ها، ساختارها و رابط‌ها قرار می‌گیرند. متغیرهای عضو یا متدها در یک کلاس یا ساختار فیلدها (Fields) گفته می‌شود. خواص به عنوان یک توسعه برای فیلدها هستند و با استفاده از همان نحو دسترسی مورد استفاده قرار می‌گیرند. آن‌ها از طریق توابع دسترسی (Accessors) استفاده می‌کنند که از طریق آن‌ها مقادیر فیلدهای خصوصی می‌توانند خوانده، نوشته یا تغییر داده شوند.

خواص مکان‌های ذخیره‌سازی را نامگذاری نمی‌کنند. به جای آن، دارای توابع دسترسی (Accessors) هستند که مقادیر آن‌ها را خوانده، نوشته یا محاسبه می‌کنند.

به عنوان مثال، فرض کنید یک کلاس به نام دانش‌آموز داشته باشیم، با فیلدهای خصوصی برای سن، نام و کد. ما نمی‌توانیم به طور مستقیم به این فیلدها از خارج از حوزه کلاس دسترسی داشته باشیم، اما می‌توانیم خواصی برای دسترسی به این فیلدهای خصوصی داشته باشیم.

توابع دسترسی (Accessors)

تابع دسترسی (Accessor) یک خاصیت حاوی دستورالعمل‌های قابل اجرا است که در خواندن (خواندن یا محاسبه) یا نوشتن خاصیت کمک می‌کند. تعریف توابع دسترسی می‌تواند شامل تابع دسترسی دریافت (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 tutorialspoint {
   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 = "001";
         s.Name = "Zara";
         s.Age = 9;
         Console.WriteLine("Student Info: {0}", s);
         
         //let us increase age
         s.Age += 1;
         Console.WriteLine("Student Info: {0}", s);
         Console.ReadKey();
      }
   }
}

هنگامی که کد بالا کامپایل و اجرا می شود، نتیجه زیر را ایجاد می کند -

Student Info: Code = 001, Name = Zara, Age = 9
Student Info: Code = 001, Name = Zara, Age = 10

خواص انتزاعی (Abstract Properties)

یک کلاس انتزاعی ممکن است دارای یک خاصیت انتزاعی باشد که باید در کلاس مشتق پیاده‌سازی شود. برنامه زیر این مورد را نشان می‌دهد −

using System;

namespace tutorialspoint {
   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 = "001";
         s.Name = "Zara";
         s.Age = 9;
         Console.WriteLine("Student Info:- {0}", s);
         
         //let us increase age
         s.Age += 1;
         Console.WriteLine("Student Info:- {0}", s);
         Console.ReadKey();
      }
   }
}

هنگامی که کد بالا کامپایل و اجرا می شود، نتیجه زیر را ایجاد می کند -

Student Info: Code = 001, Name = Zara, Age = 9
Student Info: Code = 001, Name = Zara, Age = 10