From: Pixel <pixel@mandrakesoft.com>
To: jweirich@one.net, Chris_M._Rathman@dresser.com
Cc: caml-list@inria.fr
Subject: ocaml example at http://w3.one.net/~jweirich/oostuff/
Date: 01 Jul 2000 01:09:30 +0200 [thread overview]
Message-ID: <ly1z1eu5p1.fsf@leia.mandrakesoft.com> (raw)
(*
Here is little rework of the example that i find nicer :)
- it now shows the ability to subtype when inheriting...
- use of printf for cleanup
- use of List.iter instead of explicitly doing it via recursion
- removed the "int" typing of class constructors, caml doesn't need any help,
even if keeping them may be clearer
cu Pixel.
*)
class virtual shape initx inity =
object (self)
val mutable x = initx
val mutable y = inity
(* get the x & y coordinates for the object *)
method getX = x
method getY = y
(* set the x & y coordinates for the object *)
method setX newx = x <- newx
method setY newy = y <- newy
(* move the x & y position of the object *)
method moveTo newx newy =
self#setX newx;
self#setY newy
method rMoveTo deltax deltay =
self#setX (self#getX + deltax);
self#setY (self#getY + deltay)
(* draw method is effectively abstract *)
method virtual draw : unit
end
class rectangle initx inity initwidth initheight =
object (self)
inherit shape initx inity
val mutable width = initwidth
val mutable height = initheight
(* get the width & height of the object *)
method getWidth = width
method getHeight = height
(* set the width & height of the object *)
method setWidth newwidth = width <- newwidth
method setHeight newheight = height <- newheight
(* draw the rectangle *)
method draw =
Printf.printf "Drawing a Rectangle at:(%d,%d), width %d, height %d\n"
self#getX
self#getY
self#getWidth
self#getHeight
end
class circle initx inity initradius =
object (self)
inherit shape initx inity
val mutable radius = initradius
(* get the radius of the object *)
method getRadius = radius
(* set the radius of the object *)
method setRadius newradius = radius <- newradius
(* draw the circle *)
method draw =
Printf.printf "Drawing a Circle at:(%d,%d), radius %d\n"
self#getX
self#getY
self#getRadius
end
let main () =
(* set up lists to hold the shapes *)
let (scribble : shape list) = [
(new rectangle 10 20 5 6 :> shape) ;
(new circle 15 25 8 :> shape)
] in
(* iterate through the lists and handle shapes polymorphically *)
List.iter (fun o ->
o#draw;
o#rMoveTo 100 100;
o#draw
) scribble;
(* call a rectangle specific instance *)
let arectangle = new rectangle 0 0 15 15 in
arectangle#draw;
arectangle#setWidth 30;
arectangle#draw;;
main ();;
reply other threads:[~2000-07-01 8:47 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ly1z1eu5p1.fsf@leia.mandrakesoft.com \
--to=pixel@mandrakesoft.com \
--cc=Chris_M._Rathman@dresser.com \
--cc=caml-list@inria.fr \
--cc=jweirich@one.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox