| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]--><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="Asciidoctor 1.5.4"><title>groovy_learn</title><link rel="stylesheet" href="./asciidoctor.css">
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
- <link rel="stylesheet" href="./coderay-asciidoctor.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.css"></head><body class="article toc2 toc-left"><div id="header"><div id="toolbar"><a href="https://github.com/jMonkeyEngine/wiki/edit/master/src/docs/asciidoc/jme3/scripting/groovy_learn.adoc"><i class="fa fa-pencil-square" aria-hidden="true"></i></a><a href="https://github.com/jMonkeyEngine/wiki/new/master/src/docs/asciidoc/jme3/scripting/"><i class="fa fa-plus-square" aria-hidden="true"></i></a><input dir="auto" style="position: relative; vertical-align: top;" spellcheck="false" autocomplete="off" class="searchbox__input aa-input" id="doc-search" name="search" placeholder="Search in the doc" required="required" type="search"></div><h1>groovy_learn</h1><div class="details"><span class="author" id="author"></span><br><span id="revnumber">version ,</span> <span id="revdate">2016/03/17 20:48</span></div><div id="toc" class="toc2"><div id="toctitle">Table of Contents</div><ul class="sectlevel1"><li><a href="#learn-groovy">LEARN GROOVY</a><ul class="sectlevel2"><li><a href="#syntax">Syntax</a></li><li><a href="#highlight-language-features">Highlight language features</a></li><li><a href="#closure">Closure</a></li><li><a href="#and-gotchas">and Gotchas</a></li><li><a href="#meta-programming">Meta-programming</a></li><li><a href="#groovy-builder-swingbuilder">Groovy Builder – SwingBuilder</a></li><li><a href="#language-comperation">Language comperation</a></li></ul></li></ul></div></div><div id="content"><div class="sect2"><h3 id="learn-groovy">LEARN GROOVY</h3><div class="sect2"><h3 id="syntax">Syntax</h3><div class="paragraph"><p>Groovy’s syntax can be made far more compact than Java. For example, a declaration in Standard Java 5+ such as:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"> <span class="keyword">for</span> (<span class="predefined-type">String</span> it : <span class="keyword">new</span> <span class="predefined-type">String</span><span class="type">[]</span> {<span class="string"><span class="delimiter">"</span><span class="content">Rod</span><span class="delimiter">"</span></span>, <span class="string"><span class="delimiter">"</span><span class="content">Carlos</span><span class="delimiter">"</span></span>, <span class="string"><span class="delimiter">"</span><span class="content">Chris</span><span class="delimiter">"</span></span>})
- <span class="keyword">if</span> (it.length() <= <span class="integer">4</span>)
- <span class="predefined-type">System</span>.out.println(it);</code></pre></div></div>
- <div class="paragraph"><p>can be expressed in Groovy as:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"> [<span class="string"><span class="delimiter">"</span><span class="content">Rod</span><span class="delimiter">"</span></span>, <span class="string"><span class="delimiter">"</span><span class="content">Carlos</span><span class="delimiter">"</span></span>, <span class="string"><span class="delimiter">"</span><span class="content">Chris</span><span class="delimiter">"</span></span>].findAll{it.size() <= <span class="integer">4</span>}.each{println it}</code></pre></div></div>
- <div class="paragraph"><p>What you see in the parentheses \{ \} is a magical *Closure * . What make Groovy so powerful compare to Java!
- We will learn about it later.</p></div>
- <div class="sect3"><h4 id="official-syntax-description">Official syntax description</h4><div class="paragraph"><p><a href="http://groovy-lang.org/For+those+new+to+both+Java+and+Groovy">http://groovy-lang.org/For+those+new+to+both+Java+and+Groovy</a></p></div>
- <div class="paragraph"><p><a href="http://groovy-lang.org/User+Guide">http://groovy-lang.org/User+Guide</a></p></div>
- <div class="paragraph"><p><a href="http://groovy-lang.org/Logical+Branching">http://groovy-lang.org/Logical+Branching</a></p></div>
- <div class="paragraph"><p><a href="http://groovy-lang.org/Looping">http://groovy-lang.org/Looping</a></p></div>
- <div class="paragraph"><p>For your overview, I will introduce the most important syntax of the language:</p></div></div>
- <div class="sect3"><h4 id="logical-branching">Logical Branching</h4></div>
- <div class="sect3"><h4 id="looping">Looping</h4></div>
- <div class="sect3"><h4 id="operation">Operation</h4></div></div>
- <div class="sect2"><h3 id="highlight-language-features">Highlight language features</h3><div class="paragraph"><p>Beside with elegant syntax, Groovy has features you always wish that Java have, below paragraph will call out few features that actually help a lot in Game programming:</p></div>
- <div class="sect3"><h4 id="operation-overload">Operation overload</h4></div>
- <div class="sect3"><h4 id="collections">Collections</h4></div>
- <div class="sect3"><h4 id="reflection">Reflection</h4></div></div>
- <div class="sect2"><h3 id="closure">Closure</h3><div class="paragraph"><p>Read more about it here:</p></div>
- <div class="paragraph"><p><a href="http://groovy-lang.org/Closures">http://groovy-lang.org/Closures</a>
- link:http://groovy-lang.org/Closures+-Informal+Guide[http://groovy-lang.org/Closures-Informal+Guide]
- link:http://groovy-lang.org/Closures-Formal+Definition[http://groovy-lang.org/Closures-+Formal+Definition]</p></div>
- <div class="paragraph"><p>What is a <strong>Closure</strong>?</p></div>
- <div class="paragraph"><p>A Groovy Closure is like a “code block or a method pointer. It is a piece of code that is defined and then executed at a later point. It has some special properties like implicit variables, support for currying and support for free variables (which we’ll see later on). We’ll ignore the nitty gritty details for now (see the formal definition if you want those) and look at some simple examples.
- Simple Example</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">def clos = { println <span class="string"><span class="delimiter">"</span><span class="content">hello!</span><span class="delimiter">"</span></span> }
- println <span class="string"><span class="delimiter">"</span><span class="content">Executing the Closure:</span><span class="delimiter">"</span></span>
- clos() <span class="comment">//prints "hello!"</span></code></pre></div></div>
- <div class="paragraph"><p>Note that in the above example, “hello! is printed when the Closure is called, not when it is defined.</p></div></div>
- <div class="sect2"><h3 id="and-gotchas">and Gotchas</h3><div class="paragraph"><p><a href="http://groovy-lang.org/Differences+from+Java">http://groovy-lang.org/Differences+from+Java</a></p></div></div>
- <div class="sect2"><h3 id="meta-programming">Meta-programming</h3><div class="paragraph"><p>Here come the Good! For those who remember the power of JavaScript’s prototype keyword, you can do them same with Groovy. For those who don’t even know what JavaScript is, Meta-programming is the way to expand the class with methods and attributes on the fly.</p></div></div>
- <div class="sect2"><h3 id="groovy-builder-swingbuilder">Groovy Builder – SwingBuilder</h3><div class="paragraph"><p>Groovy use a lot power the of Builder pattern.</p></div>
- <div class="paragraph"><p>In the snippets below I will show you write a few builders to speed up your JME code. The full list of Builder come in Part3 and 4.</p></div></div>
- <div class="sect2"><h3 id="language-comperation">Language comperation</h3><div class="sect3"><h4 id="javascript-comperation">JavaScript comperation</h4></div></div></div></div><div id="footer"><div id="footer-text">Version <br>Last updated 2018-11-20 05:41:31 +00:00</div></div><script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script><script>docsearch({
- apiKey: 'a736b6d93de805e26ec2f49b55013fbd',
- indexName: 'jmonkeyengine',
- inputSelector: '#doc-search',
- debug: false // Set debug to true if you want to inspect the dropdown
- });</script></body></html>
|