tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
33
Task/Polymorphism/OCaml/polymorphism.ocaml
Normal file
33
Task/Polymorphism/OCaml/polymorphism.ocaml
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
class point ?(x=0.0) ?(y=0.0) () = (* extra () used to erase the optional parameters *)
|
||||
object (self)
|
||||
val mutable x = x
|
||||
val mutable y = y
|
||||
|
||||
method x = x
|
||||
method y = y
|
||||
method set_x x' = x <- x'
|
||||
method set_y y' = y <- y'
|
||||
method print = Printf.sprintf "Point (%f, %f)" x y
|
||||
method copy = {< >}
|
||||
end
|
||||
|
||||
class circle ?(r=1.0) ?(x=0.0) ?(y=0.0) () =
|
||||
object (self)
|
||||
inherit point ~x:x ~y:y ()
|
||||
val mutable r = r
|
||||
|
||||
method r = r
|
||||
method set_r r' = r <- r'
|
||||
method print = Printf.sprintf "Circle (%f, %f, %f)" r x y
|
||||
end
|
||||
|
||||
let print x = print_endline x#print
|
||||
|
||||
let () =
|
||||
let p = new point () in
|
||||
let c = new circle () in
|
||||
print c;
|
||||
print p;
|
||||
c#set_x 10.0;
|
||||
print c;
|
||||
print (new point ~y:2.1 ())
|
||||
Loading…
Add table
Add a link
Reference in a new issue