package main import "fmt" type Stu struct { Name string Age int } func (p *Stu) SetName(name string) *Stu { p.Name = name return p } func (p *Stu) SetAge(age int) *Stu { p.Age = age return p } func (p *Stu) Print() { fmt.Printf("age:%d, name:%s\n", p.Age, p.Name) } func main() { stu := &Stu{} stu.SetAge(12).SetName("baylor").Print()//这里就可以实现链式的调用 }
原文地址:https://www.cnblogs.com/baylorqu/p/10001812.html
时间: 2025-01-11 22:34:09