Σάββατο 16 Μαρτίου 2019

Multiple Inheritance (Changed from Version 10)

Multiple Inheritance, no need for virtual Inheritance.
A class is a global function which return a new group (the user object). A module with same name as the class is the constructor. Label class: make the members of the class after it to be used only in the group inside class definition and not listed as members returned to final object. So constructors at Mammal, WingedAnimanl and Bat are used once, and not included in Bat group.
this=Animal() merge Animal() to this

In Bat constructor we merge two times the this object, one with Mammal() and one with WingedAnimal(). But when we call Mammal() we get also a merged group with Animal(). And when we call WingedAnimal() we get also a merged group with Animal(). Because merging is a flat type, we get only one animal inside bat. The second animal merged with the first one.


**New for version 10 & 11***
Class Animal {
        module eat {
                Print "I am Aninal and now I eat you"
        }
}
Class Mammal as Animal {
        module breath {
                print "I am breathing now"
        }
}
Class WingedAnimal as Animal {
        module flap {
                print "I am flapping my wings now"
        }
}
Class Bat as Mammal as WingedAnimal{
}
Animal=Animal()
Mammal=Mammal()
Bat=Bat()
Bat.eat
Bat.breath
Bat.flap
Print valid(@Mammal as Animal)=True
Print valid(@bat as Animal)=True
Print valid(@bat as Mammal)=True
Print valid(@Animal as Mammal)=False
Print Animal is type Animal=True
Print Animal is type Mammal=False

A different approach, is when we use a Superclass. A group may have a link to a superclass. A superclass is a group with no visible members. We can create a group using a superclass (a group in a group may have a different superclass). A superclass can't have another superclass (for current 9.8 version of M2000 Interpreter). A Superclass may have unique members. We can change a class member eat. but we can call superclass member eat from a group which linked to sAnimal. So statement For SuperClass {.eat} call SuperClass eat member.
If we have in mammal a different superclass, from WingedAnimal, we get a bat with all members from the two classes Mammal() and WingedAnimal(), but we get a link to last superclass. We can choose the superclass using the right order on the two statements This=Mammal() and This=WingedAnimal() in Bat constructor.
Using Superclass we get the "class" members. With it,there are only instance members.

A superclass has no constructor. We can access superclass only from a group which get this superclass as own. But we can add some members, and that members may have the constructor to change state of the superclass own members. We haven't access to private members, but we have access to unique members. So unique members are not private members, but only a group which have access to superclass can use these unique members, so we can change the state of a superclass using these unique members. To make unique members we use Unique: label and all members after that label belong to unique members

Labels for changing definitions inside group are Public:, Private:, Unique: and Class:
Also we can use Final for variables and functions/modules (for these the keyword final inserted after the keywords function and module). A final member can get value once. If a member is not final but merge with another group which have a same name member but final then the value of member will be final. In another merging the changing of value ignored for final members (no error raised).


Superclass Global sAnimal{
        module eat {
                Print "I am Aninal and now I eat you"
        }
}
Class Mammal {
        module breath {
                print "I am breathing now"
        }
Class:
        module Mammal {
                this=sAnimal
        }
}
Class WingedAnimal {
        module flap {
                print "I am flapping my wings now"
        }
Class:
        module WingedAnimal {
                this=sAnimal
        }
}
Class Bat {
class:
        module Bat {
                this=Mammal() with WingedAnimal()
        }
}
Animal=sAnimal
Mammal=Mammal()
Bat=Bat()
Bat.eat
Bat.breath
Bat.flap
Print valid(@Mammal as Animal)=True
Print valid(@bat as Animal)=True
Print valid(@bat as Mammal)=True
Print valid(@Animal as Mammal)=False
Print Animal is type sAnimal=True
Print Animal is type Mammal=False


Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.