Menu
CoddyTech

الفئات المجردة

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

تجعل الطرق الافتراضية البحتة في C++ صنفًا أساسيًا مجردًا؛ وتستخدم C# التصريحات المجرّدة والتطبيقات التي تتجاوزها. يتطلب العمر الافتراضي متعدد الأشكال في C++ وجود مدمّر مناسب عند الحذف من خلال مؤشر إلى الصنف الأساسي. أما المراجع المُدارة في C# فلها نموذج تدمير مختلف.

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

مرجع C++

#include <algorithm>
#include <array>
#include <cctype>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
struct Shape { virtual double area() const=0; virtual ~Shape()=default; };
struct Triangle: Shape { int b,h; Triangle(int b,int h):b(b),h(h) {} double area() const override { return b*h/2.0; } };
int main() {
Triangle triangle(3,2); Shape& shape=triangle; cout << fixed << setprecision(1) << shape.area() << "\n";
}

تصريحات C# الداعمة

abstract class Named { public abstract string Describe(); }
class NamedCode : Named { private string code; public NamedCode(string code) { this.code = code; } public override string Describe() { return "code:" + code; } }

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

Named item = new NamedCode("A");
Console.WriteLine(item.Describe());

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

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

3.0

مخرجات C#:

code:A
challenge icon

التحدي

سهل

نفّذ الصنفين Rectangle وTriangle المشتقين من Shape المجرّد. مساحة المستطيل هي width * height. مساحة المثلث هي base * altitude / 2.0. أعد المجموع عبر مراجع Shape. الأبعاد أعداد صحيحة غير سالبة.

عدّل ملفات 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 = I(input[1]); string before1=Snapshot(a1);
var a2 = I(input[2]); string before2=Snapshot(a2);
var a3 = I(input[3]); string before3=Snapshot(a3);
Shape sh1 = new Rectangle(2,3); Shape sh2 = new Triangle(1,1); if (sh1.Area() != 6 || sh2.Area() != 0.5) throw new Exception("Shape API");
var result = Exercise.Solve(a0,a1,a2,a3);
if (Snapshot(a0) != before0) throw new Exception("Input mutation");
if (Snapshot(a1) != before1) throw new Exception("Input mutation");
if (Snapshot(a2) != before2) throw new Exception("Input mutation");
if (Snapshot(a3) != before3) throw new Exception("Input mutation");
Console.WriteLine(result.ToString("0.0", CultureInfo.InvariantCulture));
}
}
quiz iconاختبر نفسك

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

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

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