مشخصات مقاله
-
1314
-
0.0
-
2577
-
0
-
0
Propertis در CSharp
Propertisها
Propertisها اعضای نام دار کلاسها، ساختارها و رابطها هستند. متغیرها یا متدهای عضو یک کلاس یا ساختار Field نامیده میشوند. Propertisها افزونهای از فیلدها هستند که با سینتکس یکسانی میتوان به آنها دسترسی پیدا کرد. این Propertisها از طریق مقداری از فیلدهای خصوصی که میتوان آنها را خواند، نوشت یا دستکاری کرد، از اکسسورها استفاده میکنند.
Propertisها موقعیت حافظهها را نامگذاری نمیکنند؛ بلکه از طریق اکسسورها مقادیر آنها را میخوانند، مینویسند و یا محاسبه میکنند.
برای مثال فرض کنید کلاسی به نام Student داشته باشید و در این کلاس فیلدهایی خصوصی برای سن، نام و کد تعریف شده باشد. ما نمیتوانیم به صورت مستقیم از خارج از محدودهی این کلاس به این فیلدها دسترسی پیدا کنیم، بلکه باید این کار را با کمک Propertisها انجام دهیم.
اکسسورها
اکسسور یک Propertis شامل دستورات قابل اجرایی است که میتوان به کمک آن به خواندن یا محاسبه و نوشتن Propertis پرداخت. اعلان اکسسور میتواند شامل یک 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;
}
}
مثال
در مثال زیر کاربرد این Propertisها نشان داده شده است:
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
Propertisهای انتزاعی
یک کلاس انتزاعی میتواند دارای یک Propertis انتزاعی باشد که این Propertis باید در کلاس مشتق شده پیادهسازی شود. این امر در برنامهی زیر نشان داده شده است:
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