On Tue, Nov 22, 2016 at 6:23 PM, Jesse McNelis wrote:

> On 23 Nov. 2016 9:03 am, "Tong Sun" wrote:
> >
> > So, once again, thinking in OO, I'll define all of the common variables
> in base class, and common functionalities in virtual functions. How to make
> that idea work in Go?
> >
> > For the above specific code, how to easily make "func Output" works?
> >
>
> You can use functions and embedding for code reuse and interfaces for
> polymorphism.
>
> In your example you've implemented Speak() method for each type and
> defined a Speaker interface but then in Output() you don't call Speak().
>
Yeah, that Output() is the virtual function I'm implementing. In my real
code it has nothing to do with Speak() method, only to use those common
variables.

> If you have fields you want available through the Speaker interface then
> you need to define getter methods for those fields and add them to the
> interface.
>
Got it.

> You could also add a Speak() method to the Animal type which would then be
> available on any type that embeds an Animal and would have direct access to
> any fields on Animal.
>
However, different animals speak differently. I.e., it is not the fields on
Animal that matter but what outside it matter to Speak(), right? So adding
a Speak() method to the Animal doesn't make much sense, right?


> You could define a Speak() function that all the Speak() methods call for
> some common functionality.
>
> How you structure it depends on what real world problem you're trying to
> solve.
>
Probably, but just thinking in abstract terms, I thought there are some
basic principles that people can follow. Anyway, I think paraiso.marc's
methodology might be the only way.

>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to