From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA04937 for caml-red; Sat, 1 Jul 2000 10:47:59 +0200 (MET DST) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id BAA26695 for ; Sat, 1 Jul 2000 01:08:07 +0200 (MET DST) Received: from leia.mandrakesoft.com (office.mandrakesoft.com [195.68.114.34]) by nez-perce.inria.fr (8.10.0/8.10.0) with ESMTP id e5UN86127066 for ; Sat, 1 Jul 2000 01:08:07 +0200 (MET DST) Received: by leia.mandrakesoft.com (Postfix, from userid 505) id 31FE957E5; Sat, 1 Jul 2000 01:09:30 +0200 (CEST) To: jweirich@one.net, Chris_M._Rathman@dresser.com Cc: caml-list@inria.fr Subject: ocaml example at http://w3.one.net/~jweirich/oostuff/ From: Pixel Date: 01 Jul 2000 01:09:30 +0200 Message-ID: X-Mailer: Gnus v5.7/Emacs 20.7 Sender: weis@pauillac.inria.fr (* 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 ();;