Πέμπτη, 7 Σεπτεμβρίου 2017

Group Objects Animal-Dog

Clasic Animal-Dog classes example.

We make two classes. Each class is global until module where created erased. A class is a group constructor (group is the object). A group no need  a class to exist. Group are prototypes objects, all with a common interface. We can use labels Public (by default), Private and Class to separate definitions in a group to specific zones. A module with class name is the constructor (is optional). If constructor is part of a Class: zone then this exist only in construct time, and not in the final group. When we call Dog() class function, M2000 interpreter pass parameters to Dog module in a group made in this function (as we call it).
So first we make Alfa as an empty Animal() and then read Alfa.name$, and optionally we can read a boolean for .longtail (look the dot in name).
Secondly we have to change saySomething so we use an extra Group Alfa {} command for adding or changing definitions.
Last is the merging function, we put Alfa to This (This is the current group, which class return).


Class Animal {
      name$=""
      module saySomething {
            print "I am "+.name$
      }
}
Class Dog {
      longtail
Class:
      Module Dog {
            Alfa=Animal()
            Read Alfa.name$
            Read ? .longtail
            Group Alfa {
                  module saySomething {
                        print "I am "+.name$+ ", and I can bark"
                  }
            }
            This<=Alfa
      }
}

dog = Dog("Chiwawa")
dog.saySomething


Another way. Look differences. Dog class not now what group we put in, but  know that a saySomething module must be added or altered. There we use .name$ as part of final group from class.
Dog3 is a copy of Dog1 (not a reference Dog1


Class Animal {
      name$=""
      module saySomething {
            print "I am "+.name$
      }
Class:
      Module Animal (.name$) {}
}
Class Dog {
      longtail
Class:
      Module Dog (Alfa) {
            Read ? .longtail
            \\ change module in Alfa
            Group Alfa {
                  module saySomething {
                        print "I am "+.name$+ ", and I can bark"
                  }
            }
            \\ merge Alfa to This     
            This<=Alfa
      }
}

dog1 = Dog(Animal("Chiwawa"))
dog1.saySomething
dog2 = Dog(Animal("Saluki"),True)
dog2.saySomething
Print dog1.longtail, dog2.longtail
Print dog1.name$, dog2.name$
dog3=dog1
dog3.saySomething
Print dog3.longtail