CKTIS / C#
object model and composition: praktická referencia
object model and composition: praktická referencia: Objektový model spája stav, správanie, zapuzdrenie, dedičnosť a polymorfizmus.
Verzia: C# language and .NET 10 reference runtime
Syntax
// object modelObjektový model spája stav, správanie, zapuzdrenie, dedičnosť a polymorfizmus.
Preferujte kompozíciu a malé kontrakty, dedičnosť používajte pre skutočný vzťah is-a.
Virtuálne volanie v konštruktore môže pozorovať ešte nepripravený objekt.
Rovnosť, meniteľnosť a životný cyklus musia byť pre každý typ výslovné.
Úplný program skutočne používa object model and composition; presne očakávaný stdout je woof.
Príklady
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;
Animal animal = new Dog(); Console.WriteLine(animal.Speak()); abstract class Animal { public abstract string Speak(); } sealed class Dog : Animal { public override string Speak() => "woof"; }
Výsledok
woof
Úplný program skutočne používa `object model and composition`; presne očakávaný stdout je `woof`.