[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: calling overriden method from child class from class folder
From: |
Oliver Heimlich |
Subject: |
Re: calling overriden method from child class from class folder |
Date: |
Tue, 16 Feb 2016 18:49:56 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Icedove/38.5.0 |
On 16.02.2016 18:38, Carnë Draug wrote:
> On 16 February 2016 at 15:44, Julien Bect
> <address@hidden> wrote:
>>
>> Le 16/02/2016 15:26, Carn? Draug a ?crit :
>>> I came across this question [1] on stack overflow today. I found the
>>> question a bit confusing (the OP is mixing classdef documentation and
>>> class folders), but it basically boils down to "How to call a method
>>> from the parent class when the method has been overloaded"?
>>>
>>> I looked at our documentation and even on Mathworks documentation and I
>>> couldn't find a way to do it. In the end I suggested to keep the object
>>> of the parent class around in order to call its methods directly [2].
>>
>> I'm not sure I get the question correctly, but I can try to answer
>> anyway ;-)
>>
>> Assume that you have a class B, which is derived from a class A.
>>
>> Then you have toto() implemented in @A/toto() and overloaded in @B/toto().
>>
>> And the question is : if I have an object x of class B, how can I call
>> @A/toto on it ?
>
> Yes, that is the question.
>
>> The answer depends on where you want to call it from :
>>
>> a) If you in a method of class B, for instance @B/toto(), the answer is:
>> toto(x.A, ...)
>>
>
> That is what I did but I find this solution kind of ugly. You shouldn't
> have to keep x.A around to call its methods. So my question was if there's
> a more elegant way to do this.
I do this a lot in the interval package. I have a method which creates a
new object by calling the class constructor of the parent class and
passes the required properties by calling non-overridden getters from
the parent class. Then I have a clean new object of the parent class.
In the example you would do toto (convertToA (x)) and @B/convertToA()
would call A (getProperty1 (x), getProperty2 (x), …).
If you implement getters and constructors in an efficient way, there is
almost no performance impact.
Oliver