Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
37
Task/Call-an-object-method/D/call-an-object-method.d
Normal file
37
Task/Call-an-object-method/D/call-an-object-method.d
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
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!");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue