Inheritance/Single: Difference between revisions
Content added Content deleted
Line 1,180: | Line 1,180: | ||
=={{header|M2000 Interpreter}}== |
=={{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> |
<lang M2000 Interpreter> |
||
Module CheckIt { |
Module CheckIt { |
||
Class Animal { |
Class Animal { |
||
IamAnimal |
|||
Function objType$ {="Animal"} |
|||
\\ read only |
|||
} |
} |
||
Class Dog { |
Class Dog as Animal { |
||
IamDog |
|||
Function Final objType$ {="Dog"} |
|||
Module Dog { |
|||
This=Animal() |
|||
} |
|||
} |
} |
||
Class Cat { |
Class Cat as Animal{ |
||
IamCat |
|||
Function objType$ {="Cat"} |
|||
Module Cat { |
|||
\\ Without using Final in function above |
|||
M=Animal() |
|||
M=This |
|||
This=M |
|||
} |
|||
} |
} |
||
Class Labrador { |
Class Labrador As Dog { |
||
IamLabrador |
|||
Function Final objType$ {="Labrador"} |
|||
⚫ | |||
This=Dog() |
|||
} |
|||
} |
} |
||
Class Collie { |
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() |
|||
⚫ | |||
Print Valid(Animal.IamAnimal)=True, Animal.objType$()="Animal" |
|||
⚫ | |||
A=Collie() |
|||
Print a is type Dog = 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).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 |
|||
⚫ | |||
\\ 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 |
CheckIt |