pascalabcnet/PABCNetHelp/LangGuide/Generics/covariance.html
Mikhalkovich Stanislav 522b744236 Примеры для 3.9.0
CartesianPower(Self: string
Некоторые проверки именованных аргументов
2023-05-08 20:05:55 +03:00

51 lines
4.6 KiB
HTML

<html>
<head>
<object type="application/x-oleobject" classid="clsid:1e2a7bd0-dab9-11d0-b93a-00c04fc99f9e">
<param name="Keyword" value="Êîâàðèàíòíîñòü">
</object>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<title></title>
<link rel="StyleSheet" href="../../default.css">
</head>
<body>
<H1>Êîâàðèàíòíîñòü ïàðàìåòðîâ îáîáùåííûõ òèïîâ</H1>
<p>Ïàðàìåòðû îáîáùåííîãî òèïà â C# ìîãóò áûòü îáúÿâëåíû êîâàðèàíòíûìè èëè
êîíòðàâàðèàíòíûìè. Ïðè îïèñàíèè îáîáùåííîãî òèïà íà C# êîâàðèàíòíûé ïàðàìåòð
îòìå÷àåòñÿ ìîäèôèêàòîðîì out, à êîíòðàâàðèàíòíûé - ìîäèôèêàòîðîì in:</p>
<blockquote>
<pre class="wrap has-inner-focus" data-moniker=" dotnet-uwp-10.0 net-5.0 net-6.0 net-7.0 net-8.0 netcore-1.0 netcore-1.1 netcore-2.0 netcore-2.1 netcore-2.2 netcore-3.0 netcore-3.1 netframework-4.0 netframework-4.5 netframework-4.5.1 netframework-4.5.2 netframework-4.6 netframework-4.6.1 netframework-4.6.2 netframework-4.7 netframework-4.7.1 netframework-4.7.2 netframework-4.8 netframework-4.8.1 netstandard-1.0 netstandard-1.1 netstandard-1.2 netstandard-1.3 netstandard-1.4 netstandard-1.5 netstandard-1.6 netstandard-2.0 netstandard-2.1 xamarinandroid-7.1 xamarinios-10.8 xamarinmac-3.0 "><span><code class="csharp lang-csharp" data-author-content="public interface IEnumerable&lt;out T&gt; : System.Collections.IEnumerable" dir="ltr"><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> </code><code data-author-content="public interface IEnumerable&lt;out T&gt; : System.Collections.IEnumerable" dir="ltr"><span class="hljs-title">IEnumerable</span>&lt;<span class="hljs-title">out</span> <span class="hljs-title">T</span></code><code class="csharp lang-csharp" data-author-content="public interface IEnumerable&lt;out T&gt; : System.Collections.IEnumerable" dir="ltr">&gt;</code></span></pre>
<pre class="wrap has-inner-focus" data-moniker=" dotnet-uwp-10.0 net-5.0 net-6.0 net-7.0 netcore-1.0 netcore-1.1 netcore-2.0 netcore-2.1 netcore-2.2 netcore-3.0 netcore-3.1 netframework-4.0 netframework-4.5 netframework-4.5.1 netframework-4.5.2 netframework-4.6 netframework-4.6.1 netframework-4.6.2 netframework-4.7 netframework-4.7.1 netframework-4.7.2 netframework-4.8 netstandard-1.0 netstandard-1.1 netstandard-1.2 netstandard-1.3 netstandard-1.4 netstandard-1.5 netstandard-1.6 netstandard-2.0 netstandard-2.1 xamarinandroid-7.1 xamarinios-10.8 xamarinmac-3.0 "><code class="csharp lang-csharp" data-author-content="public interface IComparer&lt;in T&gt;" dir="ltr"><span><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title">IComparer</span>&lt;<span class="hljs-title">in</span> <span class="hljs-title">T</span>&gt;</span></code></pre>
</blockquote>
<p>Êîâàðèàíòíûé òèï ïîçâîëÿåò èñïîëüçîâàòü âìåñòî íåãî ëþáîé ïðîèçâîäíûé êëàññ,
à êîíòðàâàðèàíòíûé - ëþáîé íàäêëàññ ýòîãî òèïà.</p>
<p>Â PascalABC.NET ìîæíî ëèøî ïîëüçîâàòüñÿ îáîáùåííûìè òèïàìè ñ êîâàðèàíòíûìè
ïàðàìåòðàìè èç ñòàíäàðòíîé áèáëèîòåêè .NET. Íàèáîëåå ÷àñòî èñïîëüçóåìûì
îáîáùåííûì òèïîì ñ êîâàðèàíòíûìè ïàðàìåòðàìè ÿâëÿåòñÿ <span>
<code data-author-content="public interface IEnumerable&lt;out T&gt; : System.Collections.IEnumerable" dir="ltr">
<span class="hljs-title">IEnumerable</span>&lt;<span class="hljs-title">out</span>
<span class="hljs-title">T</span></code><code class="csharp lang-csharp" data-author-content="public interface IEnumerable&lt;out T&gt; : System.Collections.IEnumerable" dir="ltr">&gt;</code></span>,
êîòîðûé â PascalABC.NET ïðåäñòàâëåí òèïîì <code><strong>sequence of</strong> T</code>. </p>
<p>Îñíîâíîå èñïîëüçîâàíèå êîâàðèàíòíîñòè çäåñü çàêëþ÷àåòñÿ â òîì, ÷òî âìåñòî <code>
<strong>sequence of</strong> Base</code> ìîæíî ïîäñòàâëÿòü <code><strong>
sequence of</strong> Derived</code>, ãäå <code>Derived</code> - ïðîèçâîäíûé
êëàññ, à <code>Base</code> - áàçîâûé. Íàïðèìåð:</p>
<blockquote>
<p><code><strong>begin</strong><br>&nbsp; <strong>var</strong> a: <strong>
sequence of</strong> object;<br>&nbsp; <strong>var</strong> b: <strong>
sequence of</strong> integer;<br>&nbsp; a := b;<br>&nbsp; a := Arr(1,2,3);<br>&nbsp;
a := new List&lt;DateTime&gt;;<br><strong>end</strong>.</code></p>
</blockquote>
<p>Ñëåäóåò îáðàòèòü âíèìàíèå, ÷òî <code>
<strong>array of</strong> integer</code> ìîæíî ïðèñâîèòü ïåðåìåííîé òèïà <code>
<strong>sequence of</strong> integer</code> ñîãëàñíî ïðèíöèïó ïîäñòàíîâêè, à
<code>
<strong>sequence of</strong> integer</code> ìîæíî ïðèñâîèòü ïåðåìåííîé òèïà
<code>
<strong>sequence of</strong> object</code>&nbsp; çà ñ÷åò êîâàðèàíòíîñòè. Èìåííî
ïîýòîìó ïðèñâàèâàíèå <code>a := Arr(1,2,3)</code> äîïóñòèìî.</p>
</body>
</html>