Polymorphism: Difference between revisions

no edit summary
m (Minor fix)
No edit summary
Line 2,275:
return Circle:new{x = self.x, y = self.y, r = self.r}
end</lang>
 
 
=={{header|M2000 Interpreter}}==
For OOP in M2000 we use Groups (we can use COM objects for other reasons, but for OOP we have Groups). A Group is collection of members. We can add permanently or temporary members, but we can't delete (using temporary members, means we delete members, but it isn't the the same as a free delete of any member).
 
Classes are functions which return groups. Groups are value types, not reference, but we can use pass by reference, for group or for any member (value type plus reference to functions), and also we can use group pointers (pointers can change group which points, references can't change and always reference a named group). We can make a group from other group, just using a =.
 
Named group means a static group. Float group is a group in a container, like an array item. A pointer to group can be one of two kind, a pointer to named group and a pointer to a float group. A pointer act as container too. A group may have any level of nested groups, and some of them maybe are pointers to groups. References can't be stored, except as strings as weak references, and before use them we have to link again. Pointers which points to named groups has same issue, they use weak reference. A group pointer may change type, to float or to named group, but stay as pointer.
 
Properties are groups with values inside groups. We can use variables, but properties have a private variable [name] and has a Set and a Value part. We can define properties with Value/Set, Value, Set, or both automatic (here we do that for x,y and r)
 
We see polymorphism for print method (module in M2000), and for operator "=". Also constructor
 
<lang M2000 Interpreter>
Class PointA {
Property x=0~
Property Y=0~
Operator "=" (n1) {
n=group(n1)
if n.x=.x Then if n.y=.y then push true : exit
push false
}
Module Print {
Print "Point" , .x, .y
}
Class:
Module PointA {
\\ ? means optionaly
Read ? .[x], .[y]
}
}
Class Circle {
Property R=300~ ' type single
Operator "=" (n1) {
n=group(n1)
n2=This ' get a copy of this to check n agains n2
if valid(@n as n2) else push false :exit
if n.x=.x Then if n.y=.y then if n.r=.r then push true : exit
push false
}
Module Print {
Print "Circle", .x, .y, .r
}
Class:
Module Circle {
if match("nn") then {
M=PointA(Number, Number)
'combine This to M, Point replaced from Super
} Else.if match("G") then {
M=PointA()
Read M
} Else M=PointA()
M=This
Read M.r
This=M
}
}
A=PointA(10,3)
C=Circle(20,10,5)
D=Circle(A, 100)
B=A
K=PointA()
Z=Circle(A)
P=PointA(600,700)
N=(A, B,C, D, K,P, Z)
M=each(N)
While M {
For This {
\\ a copy in MM
MM=Array(M)
MM.Print
Print MM=A, MM=D
}
}
</lang>
 
=={{header|NetRexx}}==
Anonymous user