classes.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1">
  5. <title>Gravity: Classes</title>
  6. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css" />
  7. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  8. <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:400,700,800">
  9. <link rel="stylesheet" href="scripts/highlight/styles/github-gist.css">
  10. <link rel="stylesheet" href="stylesheets/styles.css">
  11. </head>
  12. <body>
  13. <!-- BEGIN NAVIGATION BAR -->
  14. <nav class="navbar navbar-default navbar-fixed-top">
  15. <div class="container">
  16. <div class="navbar-header">
  17. <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
  18. <span class="sr-only">Toggle navigation</span>
  19. <span class="icon-bar"></span>
  20. <span class="icon-bar"></span>
  21. <span class="icon-bar"></span>
  22. </button>
  23. <a class="navbar-brand" href="http://gravity-lang.org"><img src="images/logo.png" width="32px" height="36px"></a>
  24. </div>
  25. <div id="navbar" class="navbar-collapse collapse">
  26. <ul class="nav navbar-nav navbar-right">
  27. <li class="active"><a href="index.html">Gravity</a></li>
  28. <li><a href="internals/index.html">Internals</a></li>
  29. <li><a href="https://github.com/marcobambini/gravity">GitHub</a></li>
  30. </ul>
  31. </div><!--/.nav-collapse -->
  32. </div>
  33. </nav>
  34. <!-- END NAVIGATION BAR -->
  35. <div class="container body-container">
  36. <div class="main-content">
  37. <div class="row">
  38. <!-- BEGIN SIDEBAR -->
  39. <div class="col-sm-3 border-right section-left">
  40. <div saveheight="1" class="sidebar-nav">
  41. <h4>INTRODUCTION</h4>
  42. <ul>
  43. <li><a href="index.html"><span>Introduction</span></a></li>
  44. <li><a href="getting-started.html"><span>Getting Started</span></a></li>
  45. </ul>
  46. <h4>LANGUAGE GUIDE</h4>
  47. <ul>
  48. <li><a href="syntax.html"><span>Syntax</span></a></li>
  49. <li><a href="operators.html"><span>Operators</span></a></li>
  50. <li><a href="types.html"><span>Types</span></a></li>
  51. <li><a href="lists.html"><span>Lists</span></a></li>
  52. <li><a href="maps.html"><span>Maps</span></a></li>
  53. <li><a href="enum.html"><span>Enum</span></a></li>
  54. <li><a href="functions.html"><span>Functions</span></a></li>
  55. <li><a href="closures.html"><span>Closures</span></a></li>
  56. <li><a href="classes.html" class="active"><span>Classes</span></a></li>
  57. <li><a href="control-flow.html"><span>Control Flow</span></a></li>
  58. <li><a href="loops.html"><span>Loops</span></a></li>
  59. <li><a href="fibers.html"><span>Fibers</span></a></li>
  60. </ul>
  61. <h4>ADVANCED</h4>
  62. <ul>
  63. <li><a href="api.html"><span>Embedding API</span></a></li>
  64. <li><a href="system.html"><span>System class</span></a></li>
  65. <li><a href="math.html"><span>Math class</span></a></li>
  66. <li><a href="unit-test.html"><span>Unit test</span></a></li>
  67. <li><a href="contributing.html"><span>Contributing</span></a></li>
  68. </ul>
  69. </div>
  70. </div>
  71. <!-- END SIDEBAR -->
  72. <div class="col-sm-9 border-left section-right">
  73. <h1 class="section-header">Class</h1><hr>
  74. <p class="section-content">
  75. Every value in Gravity is an object, and every object is an instance of a class. Classes define an object's behavior and state. Behavior is defined by methods which live in the class. Every object of the same class supports the same methods. State is defined in fields, whose values are stored in each instance.<br><br>Like <a href="functions.html">functions</a> a <strong>Class is a first class object</strong>, that means that it can be stored in local variables (even in <a href="list.html">Lists</a> or <a href="map.html">Maps</a>), passed as a function parameter or returned by a function. Gravity supports <strong>nested classes</strong> and <strong>single inheritance</strong>.
  76. </p>
  77. <h4 class="section-h4">Defining a class</h4>
  78. <p>Like most programming languages the class keyword is used to declare a new class:</p>
  79. <pre><code class="swift">
  80. class Italy {
  81. }
  82. </code></pre>
  83. <h4 class="section-h4">Instantiate a class</h4>
  84. <p>A class in gravity can be instantiated by simply executing it (without the new keyword):</p>
  85. <pre><code class="swift">
  86. var instance = Italy();
  87. </code></pre>
  88. <h4 class="section-h4">Methods</h4>
  89. <p>Functions declared inside a class are called methods and are used to add behaviors to objects that belong to a specific class:</p>
  90. <pre><code class="swift">
  91. class Italy {
  92. func print() {
  93. System.print("Hello from Italy");
  94. }
  95. }
  96. </code></pre>
  97. <h4 class="section-h4">Properties</h4>
  98. <p>Variables declared inside a class are called properties and are used to add states to objects that belong to a specific class:</p>
  99. <pre><code class="swift">
  100. class Italy {
  101. var population = 60656000;
  102. var area = 301340; // in km2
  103. func density() {
  104. return population/area;
  105. }
  106. }
  107. func main() {
  108. var it = Italy();
  109. return it.density(); // returns 201.287582
  110. }
  111. </code></pre>
  112. <h4 class="section-h4">Class methods and properties</h4>
  113. <p>A class method (or property) is a method (or property) that operates on class objects rather than instances of the class. In Gravity you can specify a class method (or property) using the static keyword:</p>
  114. <pre><code class="swift">
  115. class Italy {
  116. static var population = 60656000;
  117. static var area = 301340; // in km2
  118. static func density() {
  119. return population/area;
  120. }
  121. }
  122. func main() {
  123. return Italy.density();
  124. }
  125. </code></pre>
  126. <h4 class="section-h4">Getters and Setters:</h4>
  127. <p>As a convenient way to execute some code when a property is read or written, Gravity fully support custom getters and setters:</p>
  128. <pre><code class="swift">
  129. class foo {
  130. private var _a = 12;
  131. var a {
  132. set {_a = value * 100;} // value is default parameter name
  133. get {return _a/2;}
  134. };
  135. var b {
  136. // in this case b is a write-only property
  137. set (newb) {_a = newb * 50;} // parameter name can be specified
  138. };
  139. }
  140. func main() {
  141. var f = foo();
  142. f.a = 14; // 14*100 = 1400
  143. return f.a; // 1400/2 = 700
  144. }
  145. </code></pre>
  146. <h4 class="section-h4">Adding methods at runtime:</h4>
  147. <p>Sometimes you need to add methods at runtime to a particular instance, this is far more efficient than subclassing and in many cases it could be a decision than can be applied only at runtime. Gravity provides a convenient <strong>bind</strong> method specifically developed to manage this feature:</p>
  148. <pre><code class="swift">
  149. class foo {
  150. func f1() {System.print("Hello from f1");}
  151. }
  152. func main() {
  153. var obj = foo();
  154. obj.f1(); // Output: Hello from f1
  155. // add a new f2 method to obj instance
  156. obj.bind("f2", {System.print("Hello from f2");});
  157. obj.f2(); // Output: Hello from f2
  158. // replace f1 method
  159. obj.bind("f1", {System.print("Hello from f1 new");});
  160. obj.f1(); // Output: Hello from f1 new
  161. // with unbind you can remove an existing method
  162. obj.unbind("f2");
  163. obj.f2(); // RUNTIME ERROR: Unable to find f2
  164. }
  165. </code></pre>
  166. <h4 class="section-h4">Nested classes:</h4>
  167. <p>There are many cases where nested classes can lead to more readable and maintainable code, for example as a way of logically grouping classes that are only used in one place:</p>
  168. <pre><code class="swift">
  169. class Database {
  170. public var query;
  171. class RecordSet {
  172. public var sql;
  173. public func run() {
  174. if (!sql) return 0;
  175. System.print(sql);
  176. return sql.length();
  177. }
  178. func init() {
  179. System.print("RecordSet init called");
  180. }
  181. }
  182. func init() {
  183. System.print("Database init called");
  184. query = RecordSet();
  185. }
  186. }
  187. func main() {
  188. var db = Database();
  189. db.query.sql = "Hello World from Gravity!";
  190. return db.query.run();
  191. }
  192. </code></pre>
  193. <h4 class="section-h4">Access specifiers</h4>
  194. <p>The public and private keywords can be used to restrict access to specific parts of code.</p>
  195. </div>
  196. </div> <!-- /row -->
  197. </div> <!-- /main-content -->
  198. </div> <!-- /container -->
  199. <!-- BEGIN FOOTER -->
  200. <footer class="navbar-fixed-bottom">
  201. <div class="container footer">
  202. <p>
  203. </p>
  204. </div>
  205. </footer>
  206. <!-- END FOOTER -->
  207. <!-- Bootstrap JS -->
  208. <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
  209. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  210. <!-- Highlights JS -->
  211. <script src="scripts/highlight/highlight.min.js"></script>
  212. <script>hljs.initHighlightingOnLoad();</script>
  213. </body>
  214. </html>