Inheritance/Single: Difference between revisions

Line 1,180:
 
=={{header|M2000 Interpreter}}==
There are two types of Inheritance. This is the type which we merge groups. Class functions are global functions. First a class function make a Group and then call a module, passing arguments if any, with same name as class name. The final group has no reference with any kind of class, it is an object of type Group, without pointer (we see it as a variable). We can make pointers to groups, but here we don't need that. We can place groups in containers, and here we place one in B(5). Using variables like IamAnimal (which is a double with 0 value) and check if exist (using Valid() function) we can check the Inheritance.
 
In constructor the statement This=Animal() add all members of returned group to This. If a function in This is Final then can't be changed. We can make the "copy" using a third group: M=Animal() : M=This : This= M. First we make M as copy of Animal(), after that we merge This to M, and then we merge M to This. Why to do this? Because we want to leave members in non final stage. So M get the Animal's function, then M take This function and replace animals, and then This take M which have also the IamAnimal member.
Check Cat Class.
 
 
<lang M2000 Interpreter>
Module CheckIt {
Class Animal {
IamAnimal
Function objType$ {="Animal"}
\\ read only
}
Class Dog as Animal {
IamDog
Function Final objType$ {="Dog"}
Module Dog {
This=Animal()
}
}
Class Cat as Animal{
IamCat
Function objType$ {="Cat"}
Module Cat {
\\ Without using Final in function above
M=Animal()
M=This
This=M
}
}
Class Labrador As Dog {
IamLabrador
Function Final objType$ {="Labrador"}
Module Labrador {
This=Dog()
}
}
Class Collie As Dog{
IamCollie
Function Final objType$ {="Collie"}
Module Collie {
This=Dog()
}
}
\\ a is a pointer to a group made from class Labrador
Animal=Animal()
Module a->Labrador {()
Print Valid(Animal.IamAnimal)=True, Animal.objType$()="Animal"
Print Valid(@Aa asis type Animal) = True
A=Collie()
DimPrint B(0a tois 9)type Dog = True
Print Valid(B(5).IamCat)a is type Labrador = True
B(5)=Cat()
Print Valid(A.IamAnimal)=True, Valid(B(5).IamAnimal)=True
Print Valid(A.IamDog)=True, Valid(A.IamCollie)=True
Print Valid(B(5).IamCat)=True
Print Valid(B(5).IamDog)=False
Print A.objType$()="Collie"
Print B(5).objType$()="Cat"
\\ with @ we tell to interpreter to check A if has same members of Animal among other members
Print Valid(@A as Animal)=True
\\ For expressions, or items from containers we have to use a function
\\ which copies objects before using in Valid(@..)
Def ValidObj(X,Y)=Valid(@X as Y)
Print ValidObj(B(5), Animal)=True
}
CheckIt
404

edits