Menu
CoddyTech

الخصائص تحمي الثوابت

الدرس 34 من 48 في دورة ‏C# لمطوري Luau على Coddy.

لا تملك جداول Luau صياغةً للخصائص: الأسلوب المتبع هو استخدام طريقة للحصول على القيمة مثل Balance()، أو استخدام طريقتي metamethod ‏__index و__newindex لاعتراض المفاتيح غير الموجودة. لا يزال بإمكان أي مستدعٍ إسناد قيمة إلى account._amount مباشرةً، كما أن table.freeze يمنع الكتابة على مستوى واحد فقط. تجعل خاصية C# التي لها أداة إحضار عامة ولا تملك أداة تعيين عامة المترجم يرفض الإسناد من الخارج. لا يؤدي table.freeze ولا مرجع readonly في C# إلى تجميد الحالة المتداخلة بعمق.

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

مرجع Luau

local Account = {}
Account.__index = Account
function Account.new(n: number)
    return setmetatable({ _amount = math.max(0, n) }, Account)
end
function Account:Deposit(n: number)
    if n > 0 then
        self._amount += n
    end
end
function Account:Balance(): number
    return self._amount
end
local account = Account.new(5)
account:Deposit(-3)
print(account:Balance())

تعريفات C# المساندة

class Reading { private int raw; public Reading(int raw) { this.raw = raw; } public int Doubled { get { return raw * 2; } } }

تعليمات C# داخل Main

var reading = new Reading(3);
Console.WriteLine(reading.Doubled);

هذه مقتطفات تعليمية مركزة. تتضمن ملفات التحدي أغلفة تنفيذية كاملة؛ وتعرض المقتطفات أعلاه التعريفات والتعليمات ذات الصلة.

المخرجات المرجعية:

5

مخرجات C#:

6
challenge icon

التحدي

سهل

أكمل فئة Account: يُقيَّد الرصيد الافتتاحي بحيث لا يقل عن الصفر، وتزيده الإيداعات الموجبة، بينما تُتجاهل الإيداعات الصفرية أو السالبة. يمكن قراءة الرصيد، لكن لا يمكن تعيينه علنًا.

عدّل ملفات C# غير المقفلة. يقرأ الملف المقفَل والظاهر Main.cs الأسطر الموضحة، ويستدعي Exercise.Solve، ويتحقق من عدم تعديل المدخلات، ثم يطبع النتيجة مع محرف سطر جديد أخير واحد. أبقِ واجهات API المعلنة كما هي. لا تضف Main ثانيًا ولا تطبع مخرجات تشخيصية.

كما يختبر المشغّل المقفَل واجهة API المساعدة أو واجهة API للفئة المُصدَّرة بشكل مستقل، وليس غلاف Solve فقط.

جرّب بنفسك

using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
public class Program {
static int I(string text) { return int.Parse(text,CultureInfo.InvariantCulture); }
static int[] Ints(string text) { return text.Length==0 ? new int[0] : text.Split(',').Select(I).ToArray(); }
static string[] Words(string text) { return text.Length==0 ? new string[0] : text.Split(','); }
static string Snapshot(object value) { if (value==null) return "<null>"; if (value is string) return (string)value; var sequence=value as System.Collections.IEnumerable; if (sequence!=null) { var items=new List<string>(); foreach (object item in sequence) items.Add(Snapshot(item)); return string.Join("|",items); } return Convert.ToString(value,CultureInfo.InvariantCulture); }
public static void Main() {
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var rows = new List<string>(); string line; while ((line=Console.ReadLine()) != null) rows.Add(line);
string[] input=rows.ToArray();
var a0 = I(input[0]); string before0=Snapshot(a0);
var a1 = Ints(input[1]); string before1=Snapshot(a1);
var accountOne = new Account(-2); var accountTwo = new Account(5); accountOne.Deposit(3); accountOne.Deposit(-9); if (accountOne.Balance != 3 || accountTwo.Balance != 5) throw new Exception("Account API");
var result = Exercise.Solve(a0,a1);
if (Snapshot(a0) != before0) throw new Exception("Input mutation");
if (Snapshot(a1) != before1) throw new Exception("Input mutation");
Console.WriteLine(result);
}
}
quiz iconاختبر نفسك

يتضمن هذا الدرس اختبارًا قصيرًا. ابدأ الدرس للإجابة عليه وتتبّع تقدمك.

جميع دروس ‏C# لمطوري Luau

تدرّب بنفسك: مترجم C# عبر الإنترنت