rev 495 - trunk/docs/tutorial
SVN User <[email protected]> Mon, 17 May 2004 15:50:24 -0400
| Newsgroups | gmane.comp.lang.prothon.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: mark
Date: 2004-05-17 15:50:21 -0400 (Mon, 17 May 2004)
New Revision: 495
Modified:
trunk/docs/tutorial/index2.htm
trunk/docs/tutorial/tutorial8.htm
Log:
started Prototype-based programming section of tutorial
Modified: trunk/docs/tutorial/index2.htm
===================================================================
--- trunk/docs/tutorial/index2.htm 2004-05-17 17:45:14 UTC (rev 494)
+++ trunk/docs/tutorial/index2.htm 2004-05-17 19:50:21 UTC (rev 495)
@@ -38,9 +38,9 @@
<tr>
<td height="2203" valign="top">
<ul>
- <li>6. <a href="tutorial8.htm#myoo">Making Your Own Objects</a>
+ <li>6. <a href="tutorial8.htm#myoo">Prototype-based Programming</a>
<ul>
- <li><a href="tutorial8.htm#oaok">6.1 Object() & Object Keyword</a>
+ <li><a href="tutorial8.htm#oaok">6.1 Making Your Own Object</a>
<ul>
<li><a href="tutorial8.htm#oeof">obj = Object()</a>
</ul>
Modified: trunk/docs/tutorial/tutorial8.htm
===================================================================
--- trunk/docs/tutorial/tutorial8.htm 2004-05-17 17:45:14 UTC (rev 494)
+++ trunk/docs/tutorial/tutorial8.htm 2004-05-17 19:50:21 UTC (rev 495)
@@ -34,17 +34,62 @@
<tr>
<td height="3212" valign="top">
<p align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="myoo"></a>6.0
- Making Your Own Objects</font></p>
+ Prototype-based Programming</font></p>
<p align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="oaok"></a>6.1
- Object() and Object Keyword</font></p>
+ Making Your Own Object</font></p>
<p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="oeof"></a></font>So
far you have created many objects using special creation methods built
into the Prothon language such as integer and string and list constants.
It's time to learn how to build objects from scratch. The most fundamental
- way to build an object is to use the for "obj = Object()".
+ way to build an object is to use the form "obj = Object()".
This creates an "empty" object. It has no binary data and
- no attributes. </p>
- <p> </p>
+ no attributes. Let's create an empty object called "Rectangle":</p>
+ <pre>>>> Rectangle = Object()
+<Object:939f40>
+>>></pre>
+ <p>Prothon displays the empty object by just showing it's address in memory
+ in hexadecimal. This can be useful at times because the address of an
+ object never changes for the life of the object. When you compare two
+ objects using the "a is b" operator, Prothon just compares
+ the addresses. </p>
+ <p>If we want the new Rectangle object to represent a real rectangle,
+ we should give it a width and height. We can use attributes to do that.
+ Let's also add a method (function that uses "self", also in
+ an attribute) that calculates it's area:</p>
+ <pre>>>> Rectangle.width = 10
+10
+>>> Rectangle.height = 10
+10
+>>> def Rectangle.area():
+... return self.width * self.height
+...
+>>></pre>
+ <p>We promised you earlier that the with statement would be useful. Here
+ is our chance to use it. Let's redo that last example using the "with"
+ statement. Let's also show the area method in action:</p>
+ <pre>>>> with Rectangle:
+... width = 10
+... height = 10
+... def area():
+... return self.width * self.height
+...
+>>> print Rectangle.area()
+100
+>>></pre>
+ <p>So now we have an object called "Rectangle" that represents
+ a Rectangle by storing its width and height in attributes called "width"
+ and "height" and can calculate its own area by it's "area"
+ method that is also strored in an attribute called "area".
+ </p>
+ <p>Those of you familiar with object-oriented programming might notice
+ that this is very similar behaviour to an object instance from a class
+ that might be called "Rectangle", yet we have not defined
+ any class at all, we just created the object directly. This is much
+ simpler and more direct. This is called "object-centric" programming
+ instead of "object-oriented" programming even though it is
+ obviously "oriented" towards objects. It is just that all
+ the books and courses have already been written that say that object-oriented
+ programming has classes and it's a bit too late too argue with them.</p>
</td>
</tr>
</table>