Slide 19
Slide 19 text
e
type Cat struct{ Name string }
func (c Cat) Legs() int { return 4 }
func (c Cat) PrintLegs() { fmt.Printf("I have %d legs\n",
c.Legs()) }
type OctoCat struct{ Cat }
func (o OctoCat) Legs() int { return 5 }
func (o OctoCat) PrintLegs() { fmt.Printf("I have %d legs\n",
o.Legs()) }
func main() {
var octo OctoCat
var cat Cat
fmt.Println(cat.Legs()) // “4”
cat.PrintLegs() // “I have 4 legs”
fmt.Println(octo.Legs()) // “5”
octo.PrintLegs() // “I have 5 legs”
}