CKTIS / C#
object model and composition: odniesienie praktyczne
object model and composition: odniesienie praktyczne: Model obiektowy łączy stan, zachowanie, enkapsulację, dziedziczenie i polimorfizm.
Wersja: C# language and .NET 10 reference runtime
Składnia
// object modelModel obiektowy łączy stan, zachowanie, enkapsulację, dziedziczenie i polimorfizm.
Preferuj kompozycję i małe kontrakty, a dziedziczenie dla prawdziwej relacji is-a.
Wirtualne wywołania w konstruktorze mogą obserwować niegotowy obiekt.
Równość, mutowalność i cykl życia muszą być jawne dla każdego typu.
Kompletny program rzeczywiście używa object model and composition; oczekiwany stdout to dokładnie woof.
Przykłady
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"; }
Wynik
woof
Kompletny program rzeczywiście używa `object model and composition`; oczekiwany stdout to dokładnie `woof`.