On 23 Nov. 2016 9:03 am, "Tong Sun" <suntong...@gmail.com> 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().

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.

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.

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.

-- 
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