From 18870edf387329f83e8c25a5b7b659f691dc2ffd Mon Sep 17 00:00:00 2001 From: wandoubaba Date: Fri, 15 Nov 2024 15:44:26 +0800 Subject: [PATCH] class --- bilibili/aceid/struct/class/test.go | 48 +++++++++++++++++++++++++++++ bilibili/aceid/struct/test1/test.go | 26 ++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 bilibili/aceid/struct/class/test.go create mode 100644 bilibili/aceid/struct/test1/test.go diff --git a/bilibili/aceid/struct/class/test.go b/bilibili/aceid/struct/class/test.go new file mode 100644 index 0000000..24430b5 --- /dev/null +++ b/bilibili/aceid/struct/class/test.go @@ -0,0 +1,48 @@ +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() +} diff --git a/bilibili/aceid/struct/test1/test.go b/bilibili/aceid/struct/test1/test.go new file mode 100644 index 0000000..d54ca9b --- /dev/null +++ b/bilibili/aceid/struct/test1/test.go @@ -0,0 +1,26 @@ +package main + +import "fmt" + +type myint int + +type Book struct { + title string + auth string +} + +func printBook(myBook Book) { + fmt.Printf("%v\n", myBook) +} + +func changeBook(myBook *Book) { + myBook.title = "Learning Golang" +} + +func main() { + var book1 Book + book1.title = "Golang" + book1.auth = "Aaron" + changeBook(&book1) + printBook(book1) +}