On Wed, Nov 23, 2016 at 3:49 PM, Tong Sun wrote: > > On Wed, Nov 23, 2016 at 12:09 PM, Nick Patavalis wrote: > >> For this specific example, something like this: >> https://play.golang.org/p/FsorWRaLKk >> > > Thanks a lot Nick! > > I've simplified it a bit. Now it is: > > func (d Dog) Output() { > // Presumably complicated stuff, not re-implemented > d.Animal.Output(d.IsA()) > } > > I'm wondering if it is possible to somehow pass d.Speak as a function > pointer to Animal.Output, so that inside Animal.Output, calling it will get > to the correct dog.Speak or cat.Speak? Here is the code to start from: > > https://play.golang.org/p/cCmum-YRX9 > > Thanks >
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() } https://github.com/suntong/lang/blob/master/lang/Go/src/oo/Polymorphism-AnimalVF2.go -- 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.