Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
November 18, 2013 Calling Base Class Overriden Methods | ||||
---|---|---|---|---|
| ||||
How do I call a parent class's overidden method? module test; abstract class SuperClass { public pure void methodA() { } } class SubClass { public override pure void methodB() { // How do I call the parent methodA() from here? } } |
November 18, 2013 Re: Calling Base Class Overriden Methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On Monday, 18 November 2013 at 19:32:39 UTC, Jeroen Bollen wrote:
> How do I call a parent class's overidden method?
super.method
so
abstract class SuperClass {
public pure void methodA() {
}
}
class SubClass : SuperClass {
public override pure void methodA() {
// calls the parents
super.methodA();
}
}
To do it from outside the class, you write the class name:
void main() {
auto obj = new SubClass();
obj.SuperClass.methodA(); // calls the specific super method
}
|
November 18, 2013 Re: Calling Base Class Overriden Methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Monday, 18 November 2013 at 19:34:56 UTC, Adam D. Ruppe wrote:
> On Monday, 18 November 2013 at 19:32:39 UTC, Jeroen Bollen wrote:
>> How do I call a parent class's overidden method?
>
> super.method
>
> so
>
> abstract class SuperClass {
> public pure void methodA() {
>
> }
> }
>
> class SubClass : SuperClass {
> public override pure void methodA() {
> // calls the parents
> super.methodA();
> }
> }
>
>
> To do it from outside the class, you write the class name:
>
>
> void main() {
> auto obj = new SubClass();
> obj.SuperClass.methodA(); // calls the specific super method
> }
Thanks! :D
Why aren't these things in the documentation? :/
|
November 18, 2013 Re: Calling Base Class Overriden Methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On Monday, 18 November 2013 at 19:36:07 UTC, Jeroen Bollen wrote:
> Why aren't these things in the documentation? :/
blargh, i thought it was, but can't find it either....
|
November 18, 2013 Re: Calling Base Class Overriden Methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On Mon, Nov 18, 2013 at 08:36:06PM +0100, Jeroen Bollen wrote: [...] > Why aren't these things in the documentation? :/ Submit a bug to the bugtracker with "[dox]" in its subject line. This is the only way to guarantee the docs will improve. T -- Answer: Because it breaks the logical sequence of discussion. Question: Why is top posting bad? |
November 19, 2013 Re: Calling Base Class Overriden Methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On Monday, 18 November 2013 at 19:36:07 UTC, Jeroen Bollen wrote: > On Monday, 18 November 2013 at 19:34:56 UTC, Adam D. Ruppe wrote: >> To do it from outside the class, you write the class name: >> >> void main() { >> auto obj = new SubClass(); >> obj.SuperClass.methodA(); // calls the specific super method >> } > > Thanks! :D > > Why aren't these things in the documentation? :/ http://dlang.org/function#virtual-functions ========================= To avoid dynamic binding on member function call, insert base class name before the member function name. For example: ... void main() { auto d = new D(); assert(d.foo() == 3); // calls D.foo assert(d.B.foo() == 1); // calls B.foo assert(d.C.foo() == 2); // calls C.foo d.test(); } ========================= Kenji Hara |
Copyright © 1999-2021 by the D Language Foundation