Download here - The Caml language
Transcript
68 method balance : Euro.c method deposit : Euro.c -> unit method withdraw : Euro.c -> Euro.c val mutable balance : Euro.c end # let c = new account in c # deposit (euro 100.); c # withdraw (euro 50.);; - : Euro.c = <obj> We now refine this definition with a method to compute interest. # class account_with_interests = # object (self) # inherit account # method private interest = self # deposit (self # balance # times 0.03) # end;; class account_with_interests : object method balance : Euro.c method deposit : Euro.c -> unit method private interest : unit method withdraw : Euro.c -> Euro.c val mutable balance : Euro.c end We make the method interest private, since clearly it should not be called freely from the outside. Here, it is only made accessible to subclasses that will manage monthly or yearly updates of the account. We should soon fix a bug in the current definition: the deposit method can be used for withdrawing money by depositing negative amounts. We can fix this directly: # class safe_account = # object # inherit account # method deposit x = if zero#leq x then balance <- balance#plus x # end;; class safe_account : object method balance : Euro.c method deposit : Euro.c -> unit method withdraw : Euro.c -> Euro.c val mutable balance : Euro.c end However, the bug might be fixed more safely by the following definition: # class safe_account = # object # inherit account as unsafe # method deposit x =
Related documents
The Objective Caml system release 3.08
The Objective Caml system release 3.00 - The Caml language
The Objective Caml system release 3.01 - The Caml language
The Objective Caml system release 2.04 - The Caml language
The Objective Caml system release 2.02 - The Caml language
OCaml 4 Reference Guide
GODI User`s Manual
The Caml Light system release 0.74 - The Caml language
User`s manual in PDF - The Caml language
Mode d`emploi Emacs, complément Environnement, suite
Manual - Ocamlviz
Lucid Synchrone Manual