2015-07-25 18:11:53 +00:00
|
|
|
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 {
|
2015-07-25 18:28:28 +00:00
|
|
|
*Pet
|
2015-07-25 18:11:53 +00:00
|
|
|
|
|
|
|
currentChaseTarget *Pet
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDog(name string) *Dog {
|
2015-07-25 18:28:28 +00:00
|
|
|
return &Dog{Pet: NewPet(name)}
|
2015-07-25 18:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
2015-07-25 18:28:28 +00:00
|
|
|
fmt.Printf("%#v\n", dog)
|
|
|
|
|
|
|
|
dog.Run()
|
|
|
|
|
|
|
|
fmt.Printf("%#v\n", dog)
|
|
|
|
|
2015-07-25 18:11:53 +00:00
|
|
|
fmt.Printf("%#v\n", dog)
|
|
|
|
fmt.Printf("%#v\n", dog.Chase(cat))
|
|
|
|
|
|
|
|
fmt.Printf("%#v\n", dog)
|
|
|
|
fmt.Printf("%#v\n", cat)
|
|
|
|
}
|