Inheritance/Single: Difference between revisions
Content deleted Content added
Added Julia language |
No edit summary |
||
Line 971: | Line 971: | ||
Casey is a collie |
Casey is a collie |
||
</pre> |
</pre> |
||
=={{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 { |
|||
IamDog |
|||
Function Final objType$ {="Dog"} |
|||
Module Dog { |
|||
This=Animal() |
|||
} |
|||
} |
|||
Class Cat { |
|||
IamCat |
|||
Function objType$ {="Cat"} |
|||
Module Cat { |
|||
\\ Without using Final in function above |
|||
M=Animal() |
|||
M=This |
|||
This=M |
|||
} |
|||
} |
|||
Class Labrator { |
|||
IamLabrator |
|||
Function Final objType$ {="Labrador"} |
|||
Module Labrator { |
|||
This=Dog() |
|||
} |
|||
} |
|||
Class Collie { |
|||
IamCollie |
|||
Function Final objType$ {="Collie"} |
|||
Module Collie { |
|||
This=Dog() |
|||
} |
|||
} |
|||
Animal=Animal() |
|||
Print Valid(Animal.IamAnimal)=True, Animal.objType$()="Animal" |
|||
A=Collie() |
|||
Dim Base 0, B(10) |
|||
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 |
|||
</lang> |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |