CKTIS / C#
object model and composition: praktická reference
object model and composition: praktická reference: Objektový model spojuje stav, chování, zapouzdření, dědičnost a polymorfismus.
Verze: C# language and .NET 10 reference runtime
Syntaxe
// object modelObjektový model spojuje stav, chování, zapouzdření, dědičnost a polymorfismus.
Preferujte kompozici a malé kontrakty, dědičnost používejte pro skutečný vztah is-a.
Virtuální volání v konstruktoru může pozorovat ještě nepřipravený objekt.
Rovnost, měnitelnost a životní cyklus musí být pro každý typ výslovné.
Úplný program skutečně používá object model and composition; přesně očekávaný stdout je woof.
Pří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ýsledek
woof
Úplný program skutečně používá `object model and composition`; přesně očekávaný stdout je `woof`.