Call an object method: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 291:
Value of x^2: 100
</pre>
 
 
=={{header|D}}==
<syntaxhighlight lang="d">struct Cat {
static int staticMethod() {
return 2;
}
 
string dynamicMethod() { // Never virtual.
return "Mew!";
}
 
class Dog {
static int staticMethod() {
return 5;
}
 
string dynamicMethod() { // Virtual method.
return "Woof!";
}
 
void main() {
// Static methods calls:
assert(Cat.staticMethod() == 2);
assert(Dog.staticMethod() == 5);
 
Cat c; // This is a value on the stack.
Dog d; // This is just a reference, set to null.
 
// Other static method calls, discouraged:
assert(c.staticMethod() == 2);
assert(d.staticMethod() == 5);
 
// Instance method calls:
assert(c.dynamicMethod() == "Mew!");
d = new Dog;
assert(d.dynamicMethod() == "Woof!");
}</syntaxhighlight>
 
=={{header|Delphi}}==
Line 377 ⟶ 417:
Elapsed Time: 10.571 ms.
</pre>
 
 
=={{header|D}}==
<syntaxhighlight lang="d">struct Cat {
static int staticMethod() {
return 2;
}
 
string dynamicMethod() { // Never virtual.
return "Mew!";
}
 
class Dog {
static int staticMethod() {
return 5;
}
 
string dynamicMethod() { // Virtual method.
return "Woof!";
}
 
void main() {
// Static methods calls:
assert(Cat.staticMethod() == 2);
assert(Dog.staticMethod() == 5);
 
Cat c; // This is a value on the stack.
Dog d; // This is just a reference, set to null.
 
// Other static method calls, discouraged:
assert(c.staticMethod() == 2);
assert(d.staticMethod() == 5);
 
// Instance method calls:
assert(c.dynamicMethod() == "Mew!");
d = new Dog;
assert(d.dynamicMethod() == "Woof!");
}</syntaxhighlight>
 
 
465

edits