rev 499 - in trunk: docs/tutorial pr/tutorial
SVN User <[email protected]> Tue, 18 May 2004 01:22:20 -0400
| Newsgroups | gmane.comp.lang.prothon.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: mark
Date: 2004-05-18 01:22:17 -0400 (Tue, 18 May 2004)
New Revision: 499
Added:
trunk/docs/tutorial/tutorial9.htm
trunk/pr/tutorial/tut11.pr
Modified:
trunk/docs/tutorial/tutorial8.htm
Log:
added init_ and sub-types to tutorial
Modified: trunk/docs/tutorial/tutorial8.htm
===================================================================
--- trunk/docs/tutorial/tutorial8.htm 2004-05-18 03:42:07 UTC (rev 498)
+++ trunk/docs/tutorial/tutorial8.htm 2004-05-18 05:22:17 UTC (rev 499)
@@ -25,7 +25,7 @@
Page</a></td>
<td width="183" valign="middle" align="center" height="25"><a href="index.htm">Tutorial
Outline</a></td>
- <td width="139" valign="middle" align="center" height="25"><a href="index.htm">Next
+ <td width="139" valign="middle" align="center" height="25"><a href="tutorial9.htm">Next
Page</a></td>
</tr>
</table>
@@ -187,9 +187,44 @@
data since we lookup all attributes in the prototype parents. This is
not done in object-oriented languages and it is a powerful advantage
of prototype-based languages. Let's continue the example:</p>
- <p> </p>
- <p> </p>
- </td>
+ <pre>>>> rect2 = Rectangle()
+>>> rect2.width = 33
+>>> rect2.height = 12
+>>> print rect2.attrs()
+{'width':33, 'height':12}
+>>> print rect2.area()
+396
+>>></pre>
+ <p>Now we have a rectangle object with it's own width and height attributes
+ only, and it behaves as if it had an area method, because it has the
+ prototype "Rectangle". Also, if we were to change the code
+ of the method "area" in the "Rectangle" prototype,
+ then the behaviour would change in rect2 also. </p>
+ <p>You can see that we could make a thousand objects that each have a
+ link to Rectangle and each object would only need to have the data that
+ is unique to that object. Also, any of the objects could have a different
+ area method for different behaviour if you wished. Prototype-based programming
+ is very flexible compared to class-based programming in addition to
+ being simpler.</p>
+ <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="oaok"></a>6.3
+ Object Statement</font></p>
+ <p>There is a convenient statement, the "Object" statement,
+ that combines the creation of the object and the with statement. The
+ following replaces the first three code examples in this section 6:</p>
+ <pre>>>> object Rectangle:
+... width = 10
+... height = 10
+... def area():
+... return self.width * self*height
+...
+>>></pre>
+ <p>The general form of the Object statement is "object name(prototypes):
+ body". "Name" is the new object being created. "Prototypes"
+ is the list of prototypes for the object. If the list is missing, as
+ in this example, then just Object is assumed. "body" is the
+ exact same body that is in the "with" statement that is executed
+ exactly once immediately.</p>
+ </td>
</tr>
</table>
<table width="630" border="0" cellpadding="0" cellspacing="0">
@@ -200,7 +235,7 @@
Page</a></td>
<td width="183" valign="middle" align="center" height="25"><a href="index.htm">Tutorial
Outline</a></td>
- <td width="139" valign="middle" align="center" height="25"><a href="index.htm">Next
+ <td width="139" valign="middle" align="center" height="25"><a href="tutorial9.htm">Next
Page</a></td>
</tr>
</table>
Added: trunk/docs/tutorial/tutorial9.htm
===================================================================
--- trunk/docs/tutorial/tutorial9.htm 2004-05-18 03:42:07 UTC (rev 498)
+++ trunk/docs/tutorial/tutorial9.htm 2004-05-18 05:22:17 UTC (rev 499)
@@ -0,0 +1,175 @@
+<html>
+<head>
+<title>Prothon Tutorial - 6.0</title>
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
+</head>
+
+<body bgcolor="#FFFFFF" text="#000000">
+<div align="center">
+ <table width="630" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFCC">
+ <tr>
+ <td height="121" width="109"><img src="../images/logo_left.jpg" width="109" height="135"></td>
+ <td valign="center" height="43">
+ <div align="center"><font size="5" face="Verdana, Arial, Helvetica, sans-serif">Prothon
+ Tutorial</font></div>
+ </td>
+ <td valign="center" height="121" width="106"><img src="../images/logo_right.jpg" width="109" height="135"></td>
+ </tr>
+ </table>
+ <br>
+ <table width="630" border="0" cellpadding="0" cellspacing="0">
+ <tr>
+ <td width="114" height="25" valign="top"> <a href="http://prothon.org">Prothon
+ Home</a></td>
+ <td width="194" valign="middle" align="center" height="25"><a href="tutorial8.htm">Previous
+ Page</a></td>
+ <td width="183" valign="middle" align="center" height="25"><a href="index.htm">Tutorial
+ Outline</a></td>
+ <td width="139" valign="middle" align="center" height="25"><a href="index.htm">Next
+ Page</a></td>
+ </tr>
+ </table>
+ <br>
+ <table width="630" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffee" height="1707">
+ <tr>
+ <td height="3212" valign="top">
+ <p align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="oaok"></a>6.4
+ Initialization</font></p>
+ <p>When you create a thousand rectangles, you don't want them to all have
+ the same width and height. So it would be nice to have a convenient
+ way to give them their dimensions when they are created. Prothon has
+ provided a standard way of doing initialization of objects. </p>
+ <p>The form of creating an object that we gave earlier is incomplete.
+ It is really "newObject = Prototype(args)". Just as when calling
+ a function, you can provide initialization parameters for object creation.
+ For this to work your "Prototype" object or one of it's prototype
+ parents must have a method called "init_" defined with the
+ formal parameters defined that match the calling "args". </p>
+ <p>When "Prototype(args)" is called, first the new object is
+ created with the parent prototype of "Prototype" and no attributes
+ or binary data. Then the method "init_" is called with this
+ new object as "self", or you could say "init_" is
+ called on the new object. If "init_" has a return statement
+ with a value, then that value becomes the final result of "Prototype(args)".
+ If there is no return value in "init_", then the "self"
+ object in "init_" becomes the final result (this is the most
+ common case).</p>
+ <pre># Prothon source file tut11.pr
+
+object Rectangle:
+ width = 0
+ height = 0
+
+ def init_(width, height):
+ self.width = width
+ self.height = height
+
+ def area():
+ return self.width * self.height
+
+rect1 = Rectangle( 5, 12)
+rect2 = Rectangle(10, 17)
+print rect1.width, rect1.height, rect1.area()
+print rect2.width, rect2.height, rect2.area()</pre>
+ <p>The output of this file is:</p>
+ <pre>5 12 60
+10 17 170 </pre>
+ <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="oaok"></a>6.4
+ Sub-types</font></p>
+ <p>Just as classes in object-oriented programming have sub-classes, prototypes
+ can have sub-types. A sub-type is simply an object that has a link to
+ a prototype but adds new methods or redefines a method. Usually a sub-type
+ is a narrowing of a category. For our example, we will use a square
+ as a sub-type of a rectangle. A square is a rectangle but it has the
+ extra restriction that width equals height. </p>
+ <pre>>>> object Square(Rectangle):
+... def init_(width):
+... Rectangle.init_{self}(width, width)
+...
+>>></pre>
+ <p>This may not be the best way to implement this, but it's an interesting
+ way. Note that when you initialize the square, it calls the rectangle
+ initialization routine with both width and height actual parameters
+ equal to the square's one width. Let's look at the call closely. </p>
+ <pre> Rectangle.init_{self}(width, width)</pre>
+ <p>First of all, the call has the Rectangle's name at the beginning. This
+ is to make sure it gets the method from the Rectangle's attributes and
+ get's that version of the method that is expecting width and height.
+ </p>
+ <p>Next notice the "{self}". If this wasn't there the self scope
+ object would default to where the "init_" function came from,
+ which would be the "Rectangle" object itself. So "init_"
+ would be called on "Rectangle" instead of the new object that
+ needs to be initialized. In general, whenever you are calling an explicit
+ version of a method from an object (a "directed" call), like
+ Rectangle.init_, you need to think about what you want the method "called
+ on" and if it isn't that object, include the {obj} where obj is
+ what you want it called on.</p>
+ <p>Finally, the arguments, (width, width) are just the normal actual argument
+ that must match the formal parameters expected in the Rectangle.init_
+ verision. Now we can use square:</p>
+ <pre>>>> print sq.width, sq.area()
+7 49
+>>></pre>
+ <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="oaok"></a>6.4
+ Changing Types</font></p>
+ <p>Now I'd like to show you something useful that no object oriented language
+ can do. Most object-oriented languages will let you change an object
+ instance's type from a sub-type to a parent type. In other words go
+ from a narrower case to a broader case. Prothon will let you change
+ types of an object in either direction. </p>
+ <p>In this example we will modify the Rectangle prototype to intelligently
+ recognize squares and then change the Rectangle object type to the more
+ specific type of Square.</p>
+ <p>Note several new things in this example. We are using the "with"
+ statement to "re-open" a prototype (Rectangle) and modify
+ an existing definition (init_) by replacing the old one. </p>
+ <p>We are using a method "setProto" which replaces all the existing
+ prototype links in an object with one new one. </p>
+ <p>We are defining a special method called "str_" that is used
+ by print and other commands to represent an object in text form. Whatever
+ string object is returned by "str_" is what will be shown
+ for that object.</p>
+ <p>In order to save typing, we'll run tut11.pr and use the console:</p>
+ <pre>C:\Prothon\pr\tutorial>prothon -i tut11.pr
+5 12 60
+10 17 170
+Prothon 0.1 Interactive Console, Build 492, May 17 2004 (Ctrl-D to exit)
+>>> with Rectangle:
+... def init_(width, height):
+... self.width = width
+... self.height = height
+... if width == height:
+... self.setProto(Square)
+...
+>>> object Square(Rectangle):
+... def init_(width):
+... Rectangle.init_{self}(width, width)
+... def str_():
+... return '<Square:'+self.width+'>'
+...
+>>> sq1 = Rectangle(10, 10) # Rectangle changes to Square here
+>>> sq2 = Square(15)
+>>> print sq1, sq1.area()
+<Square:10> 100
+>>> print sq2, sq2.area()
+<Square:15> 225
+>>></pre>
+ </td>
+ </tr>
+ </table>
+ <table width="630" border="0" cellpadding="0" cellspacing="0">
+ <tr>
+ <td width="114" height="25" valign="top"> <a href="http://prothon.org">Prothon
+ Home</a></td>
+ <td width="194" valign="middle" align="center" height="25"><a href="tutorial8.htm">Previous
+ Page</a></td>
+ <td width="183" valign="middle" align="center" height="25"><a href="index.htm">Tutorial
+ Outline</a></td>
+ <td width="139" valign="middle" align="center" height="25"><a href="index.htm">Next
+ Page</a></td>
+ </tr>
+ </table>
+</div>
+</body>
+</html>
Added: trunk/pr/tutorial/tut11.pr
===================================================================
--- trunk/pr/tutorial/tut11.pr 2004-05-18 03:42:07 UTC (rev 498)
+++ trunk/pr/tutorial/tut11.pr 2004-05-18 05:22:17 UTC (rev 499)
@@ -0,0 +1,19 @@
+
+# Prothon source file tut11.pr
+
+object Rectangle:
+ width = 0
+ height = 0
+
+ def init_(width, height):
+ self.width = width
+ self.height = height
+
+ def area():
+ return self.width * self.height
+
+rect1 = Rectangle( 5, 12)
+rect2 = Rectangle(10, 17)
+print rect1.width, rect1.height, rect1.area()
+print rect2.width, rect2.height, rect2.area()
+