Angelscript/docs/manual/doc_script_mixin.html

175 lines
7.7 KiB
HTML
Raw Permalink Normal View History

2021-04-12 18:25:02 +00:00
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.18"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>AngelScript: Mixin class</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function() { init_search(); });
/* @license-end */
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><img alt="Logo" src="aslogo_small.png"/></td>
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">AngelScript
</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.18 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('doc_script_mixin.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="PageDoc"><div class="header">
<div class="headertitle">
<div class="title">Mixin class </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>As <a class="el" href="doc_script_class_inheritance.html">multiple inheritance</a> is not available, it may sometimes be necessary to implement identical code in multiple classes. When this is necessary it is recommended to use mixin classes to avoid writing the identical code in multiple places.</p>
<p>Mixin classes allows a script to declare a partial class structure that will be included into multiple different class declarations. The mixin classes themselves are not real types and cannot be instanciated.</p>
<p>When a mixin class is included into a class declaration, the properties and methods that were declared in the mixin class will be automatically replicated into the class.</p>
<pre>
// Declare a mixin class
mixin class MyMixin
{
void SomeMethod() { property++; }
int property;
}</pre><pre> // Include the mixin class into the class to receive the methods and properties
class MyClass : MyMixin
{
int OtherMethod()
{
SomeMethod();
return property;
}
}
</pre><p>Properties and methods that have already been explicitly declared in the class will not be included again. This way the mixin class can provide a default implementation that can be overridden by the class that includes the mixin.</p>
<p>The class methods included from a mixin class will be compiled in the context of the class that included it, so it is possible for a mixin class method to refer to properties and other methods that are not declared in the mixin class if the class that includes the mixin class provides those.</p>
<pre>
mixin class MyMixin
{
void MethodA() { print("Default behaviour"); }
void MethodB() { property++; }
}</pre><pre> class MyClass : MyMixin
{
// Override the default behaviour in MethodA
void MethodA() { print("Overridden behaviour"); }</pre><pre> // Declare the property that will be used by MethodB
int property;
}
</pre><p>Mixin class methods override inherited methods from base classes, just as if the included method had been implemented in the derived class directly. Mixin class properties on the other hand are not included if the property is already inherited from a base class.</p>
<pre>
class MyBase
{
void MethodA() { print("Base behaviour"); }
int property;
}</pre><pre> mixin class MyMixin
{
void MethodA() { print("Mixin behaviour"); }
float property;
}</pre><pre> // Inheriting from base class and including mixin.
// MyClass ends up with the property from the base
// class and the method from the mixin class.
class MyClass : MyBase, MyMixin
{
}
</pre><p>A mixin class can inform a list of interfaces that has to be implemented by the script class that includes the mixin class. In this case the methods for the interfaces can optionally be provided by the mixin class itself, or left out to be implemented by the script class directly.</p>
<p>A mixin class cannot inherit from other classes.</p>
<pre>
interface I
{
void a();
void b();
}</pre><pre> mixin class M : I
{
// provide default implementation of a()
void a() { print("hello from a");</pre><pre> // leave the implementation of b() to the script class
}</pre><pre> class C : M
{
// a() is implemented by mixin class</pre><pre> // b() has to be explicitly implemented by script class
void b() { print("hello from b");
}
</pre> </div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated on Sat Dec 5 2020 23:20:24 for AngelScript by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.18 </li>
</ul>
</div>
</body>
</html>