مستخدم:Mahmoud2013/الخاصية (برمجة)

من ويكيبيديا، الموسوعة الحرة

الخاصية هي نوع خاص من أعضاء الصنف ، في بعض لغات البرمجة الموجهة للكائنات، و وسيط في البرمجة الوظيفة بين حقل (أو عضو بيانات ) طريقة . النحو للقراءة و الكتابة في الخصائص مشابها لللحقول ، ولكن عادةً قراءة وكتابة الخاصية تترجم إلى استدعاءات طريقة" جالبة " و " معيّنة ". النحو المشابه للحقل أسهل في القراءة والكتابة من العديد من استدعاءات الطريقة، ومع ذلك فإن إدخال استدعاءات الطريقة "تحت الغطاء" يسمح بالتحقق من صحة البيانات أو التحديث النشط (مثل عناصر واجهة المستخدم الرسومية ) أو تنفيذ ما يمكن تسميته "حقول للقراءة فقط ".

شاهد مثالاً إرشاديًا للغة C # أدناه.

متغيرات بناء الجملة[عدل]

تتبع بعض اللغات اصطلاحات بناءة راسخة لتحديد الخصائص والأساليب واستخدامها رسميًا.

من بين هذه الاتفاقيات:

  • تدوين النقطة
  • تدوين القوس

Dot notation

يوضح المثال التالي تدوين النقطة في JavaScript.

 document.createElement('pre');

Bracket notation يوضح المثال التالي تدوين القوس في JavaScript.

 document['createElement']('pre');

أمثلة نحوية[عدل]

سي شارب

class Pen  
{ 
  private int color; // private field
  
  // public property
  public int Color 
  { 
    get
    {
      return this.color;
    }
    set 
    {
      if (value > 0) {
        this.color = value;
      }
    }
  }
}
// accessing: 
Pen pen = new Pen();
int color_tmp = 0;
// ...
pen.Color = 17;
color_tmp = pen.Color;
// ...
pen.Color = ~pen.Color; // bitwise complement ...

// another silly example:
pen.Color += 1; // a lot clearer than "pen.set_Color(pen.get_Color() + 1)"!

تسمح الإصدارات الحديثة من C # أيضًا بـ "الخصائص التي يتم تنفيذها تلقائيًا" حيث يتم إنشاء حقل النسخ الاحتياطي للخاصية بواسطة المجمّع أثناء التجميع البرمجي. هذا يعني أن الخاصية يجب أن يكون لها معيّن. ومع ذلك ، يمكن أن تكون خاصة.

class Shape 
{
   
  public Int32 Height { get; set; }
  public Int32 Width { get; private set; }
  
}

سي ++[عدل]

لا تحتوي C ++ على خصائص من صنف الأول، ولكن هناك عدة طرق لمحاكاة الخصائص بدرجة محدودة. اثنان منها كالتالي:

#include <iostream>
  
template <typename T> class property {
    T value;
  public:
    T & operator = (const T &i) {
      return value = i;
    }
    // This template class member function template serves the purpose to make
    // typing more strict. Assignment to this is only possible with exact identical
    // types.
    template <typename T2> T2 & operator = (const T2 &i) {
      T2 &guard = value;
      throw guard; // Never reached.
    }

    // Implicit conversion back to T. 
    operator T const & () const {
      return value;
    }
};

struct Foo {
  // Properties using unnamed classes.
  class {
      int value;
    public:
      int & operator = (const int &i) { return value = i; }
      operator int () const { return value; }
  } alpha;

  class {
      float value;
    public:
      float & operator = (const float &f) { return value = f; }
      operator float () const { return value; }
  } bravo;
};

struct Bar {
  // Using the property<>-template.
  property <bool> alpha;
  property <unsigned int> bravo;
};

int main () {
  Foo foo;
  foo.alpha = 5;
  foo.bravo = 5.132f;

  Bar bar;
  bar.alpha = true;
  bar.bravo = true; // This line will yield a compile time error
           // due to the guard template member function.
  ::std::cout << foo.alpha << ", "
        << foo.bravo << ", "
        << bar.alpha << ", "
        << bar.bravo
        << ::std::endl;
  return 0;
}

C ++ و Microsoft و C ++ منشئ خاص[عدل]

مثال مأخوذ من صفحة وثائق MSDN.

// declspec_property.cpp
struct S 
{لا تحتوي C ++ على خصائص من صنف الأول، ولكن هناك عدة طرق لمحاكاة الخصائص بدرجة محدودة. اثنان منها كالتالي:
  int i;
  void putprop(int j)
  { 
   i = j;
  } 

  int getprop()
  {
   return i;
  }

  __declspec(property(get = getprop, put = putprop)) int the_prop;
};

int main()
{
  S s;
  s.the_prop = 5;
  return s.the_prop;
}

D[عدل]

class Pen
{
  private int m_ 
  
  // public get property
  public int color () {
    return m_color;
  }
  
  // public set property
  public void color (int value) {
     m_color = value;
  }
}
auto pen = new Pen;
pen.color = ~pen.color; // bitwise complement
 
// the set property can also be used in expressions, just like regular assignment
int theColor = (pen.color = 0xFF0000);

في الإصدار 2 من D ، يجب تمييز كل موصِّل أو متحور للموقع بخاصية عن طريق property@ :

class Pen
{ 
  private int m_color; // private field
  
  // public get property
  @property public int color () {
    return m_color;
  }
  
  // public set property
  @property public void color (int value) {
    m_color = value;
  }
}


Delphi/Free Pascal

type TPen = class
 private
  FColor: TColor;
  function GetColor: TColor;
  procedure SetColor(const AValue: TColor);
 public
  property Color: Integer read GetColor write SetColor;
end; 

function TPen.GetColor: TColor;
begin
 Result := FColor;
end;

procedure TPen.SetColor(const AValue: TColor);
begin
 if FColor <> AValue
  then FColor := AValue;
end;
// accessing:
var Pen: TPen;
// ...
Pen.Color := not Pen.Color;
 
(*
Delphi also supports a 'direct field' syntax -

property Color: TColor read FColor write SetColor;

or

property Color: TColor read GetColor write FColor;

where the compiler generates the exact same code as for reading and writing
a field. This offers the efficiency of a field, with the safety of a property.
(You can't get a pointer to the property, and you can always replace the member
access with a method call.)
*)

F#[عدل]

type Pen() = class
  let mutable _color = 0
 
  member this.Color
    with get() = _color
    and set value = _color <- value
end
let pen = new Pen()
pen.Color <- ~~~pen.Color

جافا سكريبت[عدل]

function Pen() {
  this._color = 0;
}
// Add the property to the Pen type itself, can also
// be set on the instance individually
Object.defineProperties(Pen.prototype, {
  color: {
    get: function () {
      return this._color;
    },
    set: function (value) {
      this._color = value;
    }
  } 
});
var pen = new Pen();
pen.color = ~pen.color; // bitwise complement
pen.color += 1; // Add one

أكشن سكربت 3.0[عدل]

class Pen
{ 
  private int $color = 1;

  function __set($property, $value)
  {
    if (property_exists($this, $property)) { 
      $this->$property = $value;
    }
  }

  function __get($property)
  {
    if (property_exists($this, $property)) {
      return $this->$property;
    }
    return null;
  }
}
var pen:Pen = new Pen();
pen.color = ~pen.color; // bitwise complement
pen.color += 1; // add one

PHP[عدل]

class Pen
{ 
  private int $color = 1;

  function __set($property, $value)
  {
    if (property_exists($this, $property)) { 
      $this->$property = $value;
    }
  }

  function __get($property)
  {
    if (property_exists($this, $property)) {
      return $this->$property;
    }
    return null;
  }
}
$p = new Pen();
$p->color = ~$p->color; // Bitwise complement
echo $p->color;

بايثون[عدل]

تعمل الخصائص بشكل صحيح فقط لأصناف النمط الجديد ( الأصناف التي تحتوي على object كصنف أصل ) ، وهي متاحة فقط في بايثون 2.2 والإصدارات الأحدث (راجع القسم ذي الصلة من البرنامج التعليمي توحيد الأنواع والفصول في بايثون 2.2 ). أضاف بايثون 2.6 بناءًا جديدًا يتضمن أدوات ديكور لتحديد الخصائص.

class Pen(object):
  def __init__(self) -> None:
    self._color = 0 # "private" variable

  @property 
  def color(self):
    return self._color

  @color.setter
  def color(self, color):
    self._color = color
pen = Pen()
# Accessing:
pen.color = ~pen.color # Bitwise complement ...

أنظر أيضا[عدل]

[[تصنيف:برمجة كائنية التوجه]]