Menu
CoddyTech

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

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

لا تحتوي Lua على خصائص أو حقول خاصة. وعادةً ما يكون المُحصِّل طريقةً عادية مثل account:balance()، ويمكن لأي مستدعٍ تجاوزها باستخدام account.amount = -100. ويتطلب اعتراض عمليات القراءة والكتابة جدولًا وسيطًا مع دوال التحويل الفوقية __index و__newindex. تُقرأ خاصية C# كما لو كانت حقلًا في موضع الاستدعاء، لكنها تشغّل مُحصِّلًا، وتُعلن عن نوعها، ويمكن الاستغناء عن مُعيّن عام لها، لذا يرفض المترجم أي إسناد من شأنه الإخلال بالثابت.

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

مرجع Lua

local Account = {}
Account.__index = Account
function Account.new(amount)
  return setmetatable({amount = math.max(0, amount)}, Account)
end
function Account:deposit(n)
  if n > 0 then self.amount = self.amount + n end
end
function Account:balance()
  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);

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

مخرجات Lua:

5

مخرجات C#:

6
challenge icon

التحدي

سهل

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

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

يختبر المشغّل المقفَل أيضًا واجهة 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# لمطوري Lua

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