fmt.Printf("[%v] %s: [%0.2f]\n", k, v.Name(), v.Area())
fmt.Printf("[%v] %s\n", k, v.Output())
On Tue, Nov 22, 2016 at 10:16 PM, Tong Sun wrote:Hi,How to architect the OO's virtual function in Go?Please take a look at this (not working) Go programPlease think of the "func Output()" as a very complicated function that I only want to define once at the base level, not to duplicate into each sub classes.How can I make it works so that the last output statement, instead of being,fmt.Printf("[%v] %s: [%0.2f]\n", k, v.Name(), v.Area())will be this instead:fmt.Printf("[%v] %s\n", k, v.Output())what about this:
On 23 Nov. 2016 9:03 am, "Tong Sun" <sunto...@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.
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().
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 a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/f_62HEOIBV4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.
I also like the approach suggested by Sebastien Binet. It's really neat in situations where you can get by without needing to override the base implementation (e.g. of Shape.Output()).
I've tried to derive how to achieve a) implementation inheritance, followed by b) type substitution and c) enabling dynamic dispatch as in virtual functions in my blog post here: https://tech.t9i.in/2014/01/22/inheritance-semantics-in-go/
Yes... so every specific animal type implements it's own Output() method, which does the trivial IsA() part, and calls Animal's Output() for the common complicated parts...
Please think of the "func Output()" as a very complicated function that I only want to define once at the base level, not to duplicate into each sub classes.
Can you make it work on play.golang.org, from this code https://play.golang.org/p/QjCtD9rGpa, according to your plan?
For this specific example, something like this: https://play.golang.org/p/FsorWRaLKk
func (a Animal) Output(s Speaker) { | |
// Complicated stuff that must not be re-implemented | |
fmt.Print("I am a ", s.IsA(), | |
". My name is ", a.Name, | |
", aged ", a.Age, | |
", it is ", a.IsMale, | |
" I am male.\n ") | |
s.Speak() | |
} |
I've tried to derive how to achieve a) implementation inheritance, followed by b) type substitution and c) enabling dynamic dispatch as in virtual functions in my blog post here: https://tech.t9i.in/2014/01/22/inheritance-semantics-in-go/What's the fundamental difference between it and paraiso.marc's implementation?My view is that GenHello() is just a fancy way of paraiso.marc's getters. True?
Hi,How to architect the OO's virtual function in Go?
I think I finally found the easiest way to make virtual function works, based on Nick Patavalis code and Tahir Hashmi's idea:
func (a Animal) Output(s Speaker) { // Complicated stuff that must not be re-implemented fmt.Print("I am a ", s.IsA(), ". My name is ", a.Name, ", aged ", a.Age, ", it is ", a.IsMale, " I am male.\n ") s.Speak() }
--