1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- let () = print_endline "Hello!";;
- (* «classes» *)
- class cat = object
- method speak = print_endline "meow"
- end
- class dog = object
- method speak = print_endline "woof"
- end
- (* «end» *)
- (* «uses» *)
- let hear_what_it_has_to_say obj =
- let () = obj#speak in ()
- (* «end» *)
- ;;
- (* «calling» *)
- let () = hear_what_it_has_to_say (new cat)
- (* prints "meow" *)
- let () = hear_what_it_has_to_say (new dog)
- (* prints "woof" *)
- (* «end» *)
- ;;
- (* «bigger-object» *)
- class cow = object
- method speak = print_endline "moo"
- method num_stomachs = 4
- end
- (* «end» *)
- ;;
- (* «call-cow» *)
- let () = hear_what_it_has_to_say (new cow)
- (* prints "moo" *)
- (* «end» *)
- ;;
- (* «speaker» *)
- let ecce_orator obj =
- let () = obj#speak in obj
- (* «end» *)
- ;;
|