mirror of
https://github.com/celisej567/wiki.git
synced 2026-01-06 10:09:54 +03:00
Talk about inheritance
This commit is contained in:
@@ -66,6 +66,23 @@ end
|
|||||||
|
|
||||||
<p><code>my_class</code> will turn into a normal class where you cannot add any more variables or functions to it. In other words, <code>my_class</code> will get "baked" and you cannot add any more ingredients to it.</p>
|
<p><code>my_class</code> will turn into a normal class where you cannot add any more variables or functions to it. In other words, <code>my_class</code> will get "baked" and you cannot add any more ingredients to it.</p>
|
||||||
|
|
||||||
|
<p>The <code>class()</code> function accepts another class as a parameter which will "inherit" the other class.</p>
|
||||||
|
|
||||||
|
<p>In simple terms, every function and variable from the other class, will be copied into yours, without having to specify them all over again.</p>
|
||||||
|
|
||||||
|
<pre><code>another_class = class(my_class) -- Inherits from my_class
|
||||||
|
|
||||||
|
another_class.MyVar = 20.0 -- Overrides the built-in variable
|
||||||
|
function another_class.NewFunc(self,b)
|
||||||
|
return self.MyVar-b
|
||||||
|
end
|
||||||
|
|
||||||
|
finish(another_class) -- Finishes it
|
||||||
|
|
||||||
|
another_class.MyFunc(5) -- Will return 25
|
||||||
|
another_class.NewFunc(5) -- Will return 15
|
||||||
|
</code></pre>
|
||||||
|
|
||||||
<hr />
|
<hr />
|
||||||
|
|
||||||
<p><a href="/wiki/QScript/Lua/Objects.html">Next -></a></p>
|
<p><a href="/wiki/QScript/Lua/Objects.html">Next -></a></p>
|
||||||
|
|||||||
@@ -25,6 +25,22 @@ Once you are done with defining your class, you call the `finish()` function lik
|
|||||||
|
|
||||||
`my_class` will turn into a normal class where you cannot add any more variables or functions to it. In other words, `my_class` will get "baked" and you cannot add any more ingredients to it.
|
`my_class` will turn into a normal class where you cannot add any more variables or functions to it. In other words, `my_class` will get "baked" and you cannot add any more ingredients to it.
|
||||||
|
|
||||||
|
The `class()` function accepts another class as a parameter which will "inherit" the other class.
|
||||||
|
|
||||||
|
In simple terms, every function and variable from the other class, will be copied into yours, without having to specify them all over again.
|
||||||
|
|
||||||
|
another_class = class(my_class) -- Inherits from my_class
|
||||||
|
|
||||||
|
another_class.MyVar = 20.0 -- Overrides the built-in variable
|
||||||
|
function another_class.NewFunc(self,b)
|
||||||
|
return self.MyVar-b
|
||||||
|
end
|
||||||
|
|
||||||
|
finish(another_class) -- Finishes it
|
||||||
|
|
||||||
|
another_class.MyFunc(5) -- Will return 25
|
||||||
|
another_class.NewFunc(5) -- Will return 15
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
[Next ->][QScript/Lua/Objects]
|
[Next ->][QScript/Lua/Objects]
|
||||||
Reference in New Issue
Block a user