1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
package main
import ( "fmt" )
type Human struct { name string age int }
type Student struct { Human school string } type Employee struct { Human company string }
func (h *Human) Info() { fmt.Printf("I am %s, %d years old\n", h.name, h.age) }
func (s *Student) Info() { fmt.Printf("I am %s, %d years old, I am a student at %s\n", s.name, s.age, s.school) } func (e *Employee) Info() { fmt.Printf("I am %s, %d years old, I am a employee at %s\n", e.name, e.age, e.company) } func main() { s1 := Student{Human{"Jack", 20}, "tsinghua"} e1 := Employee{Human{"Lucy", 26}, "Google"} s1.Info() e1.Info() }
|