Ejemplo práctico

Mensajes entre objetos.

Los mensajes entre objetos es como ya hemos dicho la comunicación que tienen ellos de comunicarse entre si, para ello hemos diseñado el siguiente ejemplo

Esta Ficha contiene como vemos unos botones con nombre de animal, hemos generado una nueva clase que se llama Perro, esta clase es invisible, no le hemos definido ninguna imagen, para comprenderlo mejor vamos a ejecutar dicho ejemplo.

Aquí hemos ejecutado el programa y hemos apretado una vez cada botón, ahora veremos la explicación del código, y todo será mas fácil  de comprender.
 

public Tobi,Lassy,Max

** END HEADER -- no elimine esta línea
//
// Generado en 11/05/98
//
parameter bModal
local f f = new perrosForm()
if (bModal)
   f.mdi = false // asegura que no es MDI
   f.readModal()
else
   f.open()
endif

class perrosForm of FORM
   with (this)
      onOpen = class::FORM_ONOPEN
      scaleFontBold = false
      height = 13.9091
      left = 32
      top = 1.7727
      width = 64.1429
      text = ""
   endwith
 

   this.EDITOR1 = new EDITOR(this)
   with (this.EDITOR1)
      height = 10.5
      left = 0
      top = 0
      width = 64
      value = ""
   endwith
 

   this.PUSHBUTTON1 = new PUSHBUTTON(this)
   with (this.PUSHBUTTON1)
      onClick = class::PUSHBUTTON1_ONCLICK
      height = 1
      left = 1
      top = 11
      width = 20
      text = "TOBI LADRA"
      value = false
   endwith
 

   this.PUSHBUTTON2 = new PUSHBUTTON(this)
   with (this.PUSHBUTTON2)
      onClick = class::PUSHBUTTON2_ONCLICK
      height = 1
      left = 22
      top = 11
      width = 20
      text = "LASSY LADRA"
      value = false
   endwith
 

   this.PUSHBUTTON3 = new PUSHBUTTON(this)
   with (this.PUSHBUTTON3)
      onClick = class::PUSHBUTTON3_ONCLICK
      height = 1
      left = 43
      top = 11
      width = 20
      text = "MAX LADRA"
      value = false
   endwith
 

   this.PUSHBUTTON5 = new PUSHBUTTON(this)
   with (this.PUSHBUTTON5)
      onClick = class::PUSHBUTTON5_ONCLICK
      height = 1.5
      left = 22
      top = 12
      width = 20
      text = "LASSY LADRA A TOBI"
      value = false
   endwith
 

   this.PUSHBUTTON6 = new PUSHBUTTON(this)
   with (this.PUSHBUTTON6)
      onClick = class::PUSHBUTTON6_ONCLICK
      height = 1.5
      left = 43
      top = 12
      width = 20
      text = "MAX LADRA A LASSY"
      value = false
   endwith
 

   this.PUSHBUTTON4 = new PUSHBUTTON(this)
   with (this.PUSHBUTTON4)
      onClick = class::PUSHBUTTON4_ONCLICK
      height = 1.5
      left = 1
      top = 12
      width = 20
      text = "LASSY LADRA A TOBI"
      value = false
   endwith
 


   function form_onOpen
        Tobi = new PERRO()
        Tobi.nombre = "TOBI"
        Lassy = new PERRO()
        Lassy.nombre = "LASSY"
        Max = new PERRO()
        Max.nombre= "MAX"
      return
 


   function PUSHBUTTON1_onClick
         form.editor1.value =form.editor1.value  +  Tobi.ladrar()
      return

   function PUSHBUTTON2_onClick
         form.editor1.value = form.editor1.value  + Lassy.ladrar()
      return

   function PUSHBUTTON3_onClick
         form.editor1.value = form.editor1.value  +  Max.ladrar()
      return

   function PUSHBUTTON4_onClick
        form.editor1.value = form.editor1.value  +  Tobi.ladrarPerro("MAX")
      return

   function PUSHBUTTON5_onClick
        form.editor1.value = form.editor1.value  +  Lassy.ladrarPerro("TOBI")
      return

   function PUSHBUTTON6_onClick
        form.editor1.value = form.editor1.value  +  Max.ladrarPerro("LASSY")
      return
endclass
 


CLASS PERRO OF OBJECT
   Nombre = ""
 


   function ladrar
      LADRIDO = chr(13)+"[ "+ this.Nombre +": GUAU]"
   return LADRIDO

   function escucha(OBJPERRO)
      LADRIDO = chr(13)+"[ "+this.nombre +": GUAU "+OBJPERRO+"]"
   return LADRIDO
     function ladrarPerro (OBJPERRO)
      LADRIDO1 = OBJPERRO + ".Escucha('"+this.nombre+"')"
      LADRIDO2 = chr(13)+"[ "+this.nombre +": GUAU "+OBJPERRO +"]"
      LADRIDO3 = &LADRIDO1
   return LADRIDO2+LADRIDO3
 


ENDCLASS

Creo que el ejemplo es muy simple, si pulso el botón TOBI LADRA, lo que hago es mostrar en el objeto Editor, el resultado de lo que me dio el Perro, si le digo LASSY LADRA MAX, lo que hace es Ladra y llama a escuchar de Max que le entrega a Lassy la respuesta a su ladrido, cuando tienen las dos cadenas me las entrega y es cuando se visualizan.

Podríamos hacer que los perros ladren solos pero si intentamos decirle que cuando ladre el que escuche ladre, entraríamos en un bucle infinito, con lo cual se bloquearía la maquina, al ser una función recursiva, y aunque dBASE soporta X recursividad, entraríamos en el típico error Demasiados UDF’s anidados.


(C) 1999 Database DM. la reproduccion total o parcial de este curso, asi como la divulgación de parte o la totalidad de este curso, esta sujeta a las leyes de Propiedad.