12345678910111213141516171819202122232425262728293031323334353637 |
- package main
- import "fmt"
- //多态
- type Live interface {
- run()
- }
- type People struct {
- name string
- }
- type Animal struct {
- name string
- }
- func (p *People) run() {
- fmt.Printf("%v在跑步", p.name)
- }
- func (a *Animal) run() {
- fmt.Printf("%v在跑步", a.name)
- }
- func allrun(live Live) {
- live.run()
- }
- func main() {
- //接口不能实例化,只能对接口的结构体实例化
- peo := &People{"derek"}
- allrun(peo) //derek在跑步
- //多态,条件不同结果不同
- a := &Animal{"小狗"}
- allrun(a) //小狗在跑步
- }
|