s.go 429 B

12345678910111213141516171819202122232425262728293031323334
  1. package model
  2. import (
  3. "fmt"
  4. )
  5. //封装
  6. type People struct {
  7. name string
  8. age int
  9. }
  10. func (p *People) SetName(name string) {
  11. p.name = name
  12. }
  13. func (p *People) SetAge(age int) {
  14. p.age = age
  15. }
  16. func (p *People) GetName() string {
  17. return p.name
  18. }
  19. func (p *People) GetAge() int {
  20. return p.age
  21. }
  22. func main() {
  23. peo := new(People)
  24. peo.SetName("derek")
  25. peo.SetAge(22)
  26. fmt.Println(peo.GetName(), peo.GetAge()) //derek 22
  27. }