الخصائص المفهرسة (F#)

الخصائص المفهرسة هي الخصائص التي توفر الوصول إلى صفيف تشبه إلى ترتيب بيانات.

// Indexed property that has both get and set defined.
member self-identifier.PropertyName
   with get(index-variable) =
      get-function-body
  and set index-variables value-variables =
      set-function-body

// Indexed property that has get only.
member self-identifier.PropertyName(index-variable) =
      get-function-body

// Alternative syntax for indexed property with get only
member self-identifier.PropertyName
   with get(index-variables) =
      get-function-body

// Indexed property that has set only.
member self-identifier.PropertyName
   with set index-variables value-variables = 
      set-function-body

ملاحظات

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

عند التي PropertyNameهو Item، يعامل المحول البرمجي للخاصية كخاصية فهرستها بشكل افتراضي. تشغيل افتراضي تمت الفهرسة الخصائص هو الوصول إلى خاصية التي يمكن أن تقوم باستخدام بناء جملة شبيه الصفيف في مثيل الكائن. على سبيل المثال، إذا كان objهو كائن من النوع الذي يعرف ترتيب هو خاصية، يكون بناء الجملة obj.[index]هو المستخدمة للوصول إلى خاصية.

بناء الجملة للوصول إلى غير افتراضي الفهرسة الخاصية هو لتوفير اسم الخاصية الفهرس في أقواس. على سبيل المثال، إذا كانت خاصية Ordinal، تقوم بكتابة obj.Ordinal(index)it. الوصول إلى

بغض النظر عن النموذج الذي تستخدمه، يجب دوماً استخدام نموذج curried setالأسلوب تشغيل عنصر الخصائص المفهرسة. للحصول على المعلومات حول الدالات curried، راجع الدالات (F#).

مثال

The following تعليمات برمجية مثال illustrates the تعريف و استخدم of الافتراضي و non-الافتراضي تمت الفهرسة خصائص that have get و set وظائف.

type NumberStrings() =
   let mutable ordinals = [| "one"; "two"; "three"; "four"; "five";
                             "six"; "seven"; "eight"; "nine"; "ten" |]
   let mutable cardinals = [| "first"; "second"; "third"; "fourth";
                              "fifth"; "sixth"; "seventh"; "eighth";
                              "ninth"; "tenth" |]
   member this.Item
      with get(index) = ordinals.[index]
      and set index value = ordinals.[index] <- value
   member this.Ordinal
      with get(index) = ordinals.[index]
      and set index value = ordinals.[index] <- value
   member this.Cardinal
      with get(index) = cardinals.[index]
      and set index value = cardinals.[index] <- value

let nstrs = new NumberStrings()
nstrs.[0] <- "ONE"
for i in 0 .. 9 do
  printf "%s " (nstrs.[i])
printfn ""

nstrs.Cardinal(5) <- "6th"

for i in 0 .. 9 do
  printf "%s " (nstrs.Ordinal(i))
  printf "%s " (nstrs.Cardinal(i))
printfn ""

الإخراج

ONE two three four five six seven eight nine ten
ONE first two second three third four fourth five fifth six 6th
seven seventh eight eighth nine ninth ten tenth

تمت الفهرسة خصائص مع Multiple فهرس متغيرات

تمت الفهرسة خصائص can have المزيد واحد فهرس متغير. في that حالة, the متغيرات are separated بواسطة الفاصلة when the خاصية هو used. The التعيين أسلوب في such a خاصية must have الثاني curried الوسيطات, the أول of which هو a tuple containing the مفاتيح, و the ثانية of which هو the القيمة being التعيين.

The following تعليمات برمجية demonstrates the استخدم of an تمت الفهرسة خاصية مع multiple فهرس متغيرات.

open System.Collections.Generic

type SparseMatrix() =
    let mutable table = new Dictionary<(int * int), float>()
    member this.Item
        with get(key1, key2) = table.[(key1, key2)]
        and set (key1, key2) value = table.[(key1, key2)] <- value

let matrix1 = new SparseMatrix()
for i in 1..1000 do
    matrix1.[i, i] <- float i * float i

راجع أيضًا:

المبادئ

الأعضاء (F#)