This commit is contained in:
wandoubaba 2024-11-15 15:44:26 +08:00
parent a0378efce0
commit 18870edf38
2 changed files with 74 additions and 0 deletions

View File

@ -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()
}

View File

@ -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)
}