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
| package main import "fmt" type AInterface interface { GetInfo() string } type BInterface interface { SetInfo(string, int) } type People struct { Name string Age int } func (p People) GetInfo() string { return fmt.Sprintf("姓名:%v 年龄:%d", p.Name, p.Age) } func (p *People) SetInfo(name string, age int) { p.Name = name p.Age = age } func main() { var people = &People{ Name: "张三", Age: 20, } var p1 AInterface = people var p2 BInterface = people fmt.Println(p1.GetInfo()) p2.SetInfo("李四", 30) fmt.Println(p1.GetInfo()) }
|