From 31975286f55ad6138c6ad3441f6315d2d53aa8df Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 25 Jul 2015 14:11:53 -0400 Subject: [PATCH] Go implementation of inheritance bits --- c&s20150725/inheritance.go | 55 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 c&s20150725/inheritance.go diff --git a/c&s20150725/inheritance.go b/c&s20150725/inheritance.go new file mode 100644 index 0000000..8a3bb62 --- /dev/null +++ b/c&s20150725/inheritance.go @@ -0,0 +1,55 @@ +package main + +import "fmt" + +type Pet struct { + Name string + IsRunning bool +} + +func NewPet(name string) *Pet { + return &Pet{Name: name} +} + +func (p *Pet) Run() string { + p.IsRunning = true + return p.Name + " is running" +} + +type Dog struct { + Pet + + currentChaseTarget *Pet +} + +func NewDog(name string) *Dog { + dog := &Dog{} + dog.Name = name + return dog +} + +func (d *Dog) Chase(target *Pet) string { + d.IsRunning = true + d.currentChaseTarget = target + d.currentChaseTarget.Run() + + return d.Name + " is chasing " + target.Name +} + +func main() { + cat := NewPet("Felix") + + fmt.Printf("%#v\n", cat) + + cat.Run() + + fmt.Printf("%#v\n", cat) + + dog := NewDog("Fido") + + fmt.Printf("%#v\n", dog) + fmt.Printf("%#v\n", dog.Chase(cat)) + + fmt.Printf("%#v\n", dog) + fmt.Printf("%#v\n", cat) +}