6. Orientación a Objetos
Conceptos clave de la orientación a objetos. Operaciones, clases, herencias y objetos.
let c1 = object val mutable n = 0 method next = n <- n + 1; n method reset = n <- 0end;;
let double_next c = c#next * 2;;
let double c = object method next = c#next * 2 method reset = c#resetend;;
class counter = object val mutable n = 0 method next = n <- n+1; n method reset = n <- 0end;;
(* CLASES 09/12 *)
let double_next o = o#next * 2;;
let c1 = object val mutable n = 0 method next = n <- n + 1; n method reset = n <- 0end;;
class counter = object val mutable n = 0 method next = n <- n+1; n method reset = n <- 0end;;
type counter = < next : int; reset : unit >
let new_counter () : counter = object val mutable n = 0 method next = n <- n+1; n method reset = n <- 0end;;
let c3 = new_counter ()
class counter_with_set = object inherit counter method set x = n <- xend;;
class counter_with_set_add_init n0 = object (self) inherit counter_with_set initializer self#set n0end;;
(*class counter_with_limit n0 max = object (this) inherit counter_with_set_add_init n0 as ancestor method next = let n = ancestor#next in if n <= max then n else (this#reset; ancestor#reset) (* MAL COPIADO ESTA LINEA NO COMPILA *)end;;*)
class counter_with_set_and_init n0 = object (self) inherit counter_with_set initializer self#set n0end;;
class counter_with_limit n0 max = object (this) inherit counter_with_set_and_init n0 as ancestor method next = let n = ancestor#next in if n <= max then n else (this#reset; ancestor#next)end;;
(* CLASES 10/12 *)
class ['a] queue = object (self) val mutable front = [] val mutable back = [] method push (e : 'a) = back <- e :: back method top = match front, back with h::_, _ -> Some h | [], [] -> None | [], _ -> front <- List.rev back; back <- []; self#top method pop = match front, back with h::t, _ -> front <- t; Some h | [], [] -> None | [], _ -> front <- List.rev back; back <- []; self#popend;;
class ['a] queue' = object (self) inherit ['a] queue method copy = Oo.copy selfend;;
(* CLASES 16/12 *)
let _ = print_char 'A' inlet _ = print_char 'B' inprint_char 'C';;
let rec output_string_list c = function [] -> () | h::t -> output_string c (h ^ "\n"); output_string_list c t;;
let rec input_string_list c = try let s = input_line c in s :: input_string_list c with End_of_file -> [];;
Pablo Portas López © 2025 licensed under CC BY 4.0