next up previous
suivant: Programme Java 1.4 monter: Comparaison Java-Ocaml: les programmes précédent: Comparaison Java-Ocaml: les programmes

Programme Ocaml

class ['a, 'b] sdd (x: 'a) =
object
  val tab = Array.make 100 x
  val mutable nb = 0
  val mutable indice = 0
  method ajouter x = Array.set tab nb x; nb <- nb + 1
  method retrouver (cle: 'b) = 
    for i = 0 to nb do
       if (Array.get tab i)#get_cle = cle then
          indice <- i
    done;
    Array.get tab indice
  method nombre_elem = nb
  method voir = for i = 0 to nb do (Array.get tab i)#print; 
      print_newline () done
end

class virtual animal (nm: string) (tat: int) =
object(moi_meme)
  val nom = nm
  val tatouage = tat
  method get_cle = tatouage
  method repond x = (nom = x)
  method virtual cri : string
  method virtual court_t_y: bool
  method virtual vole_t_y: bool
  method virtual nage_t_y: bool
  method crie = print_string moi_meme#cri
  method print = 
      print_string (nom^" "); print_int tatouage; print_newline ()
end

class robot (nm: string) (tat: int) =
object(moi_meme)
  val nom = nm
  val reference = tat
  method get_cle = reference
  method repond x = (nom = x)
  method cri  = "chiriiii"
  method court_t_y = true
  method vole_t_y = false
  method nage_t_y = false
  method crie = print_string moi_meme#cri
  method print = 
      print_string (nom^" "); print_int reference; print_newline ()
end

class chien nm tat =
object 
  inherit animal nm tat
  method cri = "ouah"
  method court_t_y = true
  method vole_t_y = false
  method nage_t_y = true
  method mord_t_y = true
end
  
class serin nm tat =
object(ce_truc)
  inherit animal nm tat
  method cri = "cuicui"
  method court_t_y = true
  method vole_t_y = true
  method nage_t_y = false
  method chante = for i = 1 to 15 do ce_truc#crie; print_string " "  done
end
  
let medor = new chien "medor" 17
;;

class animal_sdd an=
object
  inherit [animal, int] sdd an
end

let menagerie = new  sdd (medor :> animal)
;;

menagerie#ajouter (medor :> animal)
;;

for i = 1 to 10 do
  let oiseau = new serin ("serin_"^string_of_int i) (20+i)
  in
    menagerie#ajouter (oiseau :> animal)
done;;

menagerie#voir;;

let animal_25 = menagerie#retrouver 25;;

animal_25#crie; print_newline ();;



Barthelemy Francois 2004-06-01