go_study/bilibili/aceld/struct/class/test.go
2024-11-19 12:27:49 +08:00

49 lines
976 B
Go

package main
import "fmt"
// 类名和属性名首字母大写即为公有,首字母小写即为本包私有
type Hero struct {
Name string
Ad int
Level int
}
// func (this Hero) Show() {
// fmt.Println("Name = ", this.Name)
// fmt.Println("Ad = ", this.Ad)
// fmt.Println("Level = ", this.Level)
// }
// func (this Hero) GetName() string {
// fmt.Println("Name = ", this.Name)
// return this.Name
// }
// func (this Hero) SetName(name string) {
// this.Name = name
// }
// 方法名首字母大写即为公有方法,首字母小写即为私有方法
func (this *Hero) Show() {
fmt.Println("Name = ", this.Name)
fmt.Println("Ad = ", this.Ad)
fmt.Println("Level = ", this.Level)
}
func (this *Hero) GetName() string {
fmt.Println("Name = ", this.Name)
return this.Name
}
func (this *Hero) SetName(name string) {
this.Name = name
}
func main() {
hero := Hero{Name: "zhang3", Ad: 100, Level: 1}
hero.Show()
hero.SetName("Aaron")
hero.Show()
}