Menu
CoddyTech

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

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

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

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

مرجع JavaScript

class Account { constructor(n) { this.amount=Math.max(0,n); } deposit(n) { if(n>0) this.amount+=n; } get balance() { return this.amount; } }
const account=new Account(5); account.deposit(-3); console.log(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# لمطوّري JavaScript

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