pascalabcnet/PABCNetHelp/LangGuide/Interfaces/manyinterf.html

56 lines
3.1 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<html>
<head>
<object type="application/x-oleobject" classid="clsid:1e2a7bd0-dab9-11d0-b93a-00c04fc99f9e">
</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>Несколько интерфейсов могут содержать одинаковые методы или
свойства. При наследовании от таких интерфейсов такие одинаковые методы или
свойства сливаются в один:</p>
<blockquote>
<p><code><b>type</b><br>
&nbsp; IShape = <b>interface</b><br>&nbsp;&nbsp;&nbsp;
<b>procedure</b> Draw;<br>&nbsp;&nbsp;&nbsp; <b>property</b> X: integer <b>read</b>;<br>&nbsp;&nbsp;&nbsp;
<b>property</b> Y: integer <b>read</b>;<br>&nbsp;
<b>end</b>;<br>
&nbsp; IBrush = <b>interface</b><br>&nbsp;&nbsp;&nbsp;
<b>procedure</b> Draw;<br>&nbsp;&nbsp;&nbsp; <b>property</b>
Size: integer <b>read</b>;<br>&nbsp;
<b>end</b>;<br>
&nbsp; Brush = <b>class</b>(IShape,IBrush)<br>
&nbsp;&nbsp;&nbsp;
<b>procedure</b> Draw;<br>&nbsp;&nbsp;&nbsp; <strong>begin</strong><br>&nbsp;&nbsp;&nbsp;
<strong>end</strong>;<br>&nbsp; <b>end</b>;</code></p>
</blockquote>
<p>Чтобы решить проблему с одинаковыми именами в интерфейсах, в .NET классы
могут реализовывать методы интерфейсов так называемым <strong>явным</strong>
образом, так что вызов метода интерфейса для переменной класса возможен только
после явного приведения к типу интерфейса. <span id="nsrTitle">Например:</span></p>
<blockquote>
<p><code><strong>type</strong><br>&nbsp; IWindow = <strong>interface</strong><br>&nbsp;&nbsp;&nbsp;
procedure Menu;<br>&nbsp; <strong>end</strong>;<br>&nbsp; IRestaurant =
<strong>interface</strong><br>&nbsp;&nbsp;&nbsp; procedure Menu;<br>&nbsp;
<strong>end</strong>;<br>&nbsp; RestaurantSystem = <strong>class</strong>(IWindow,IRestaurant)<br>&nbsp;&nbsp;&nbsp;
<strong>public</strong><br>&nbsp;&nbsp;&nbsp; <strong>procedure</strong>
IWindow.Menu; // явная реализация метода интерфейса<br>&nbsp;&nbsp;&nbsp;
<strong>begin</strong><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Println('IWindow.Menu');<br>&nbsp;&nbsp;&nbsp; <strong>end</strong>;<br>&nbsp;&nbsp;&nbsp;
<strong>procedure</strong> IRestaurant.Menu;<br>&nbsp;&nbsp;&nbsp; <strong>
begin</strong><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Println('IRestaurant.Menu');<br>&nbsp;&nbsp;&nbsp; <strong>end</strong>;<br>&nbsp;
<strong>end</strong>;<br><br><strong>begin</strong><br>&nbsp; <strong>var</strong>
r := new RestaurantSystem;<br>&nbsp; IWindow(r).Menu; <br>&nbsp;
IRestaurant(r).Menu; <br>&nbsp; r.Menu; // ошибка компиляции!<br><strong>end</strong>. </code>
&nbsp;</p>
</blockquote>
<p>&nbsp;</p>
</body>
</html>