CKTIS / C#
interface: odniesienie praktyczne
interface: odniesienie praktyczne: interface definiuje kontrakt składowych bez tożsamości instancji i może mieć implementacje domyślne.
Wersja: C# language; verified against C# 14 reference
Składnia
interface /* grammar-specific form */interface definiuje kontrakt składowych bez tożsamości instancji i może mieć implementacje domyślne.
Stosuj do małych ról wymiennych między implementacjami.
Zmiana publicznego interfejsu wpływa na wszystkich implementatorów; default methods komplikują dispatch.
Klasa może implementować wiele interfejsów, a jawna implementacja jest dostępna tylko przez kontrakt.
Kompletny program rzeczywiście używa interface; oczekiwany stdout to dokładnie 3.
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;
IValue value = new Value(); Console.WriteLine(value.Read());
interface IValue { int Read(); } sealed class Value : IValue { public int Read() => 3; }
Wynik
3
Kompletny program rzeczywiście używa `interface`; oczekiwany stdout to dokładnie `3`.