method

method

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main

import (
	"fmt"
	"strings"
)

type MyInt int

func (mi MyInt) ToHexStr() string {
	return fmt.Sprintf("%X", mi)
}

type Student struct {
	Courses []string
	Name    string
}

func (s Student) SetCourses1(courses ...string) {
	s.Courses = courses
}

func (s Student) SetName1(name string) {
	s.Name = name
}

func (s *Student) SetCourses2(courses ...string) {
	s.Courses = courses
}

func (s *Student) SetName2(name string) {
	s.Name = name
}

func (s Student) Say1() {
	fmt.Println(s.Name, ",I had learnt courses: ", strings.Join(s.Courses, "、"), ".")
}

func (s Student) Say2() {
	fmt.Println(s.Name, ",I had learnt courses: ", strings.Join(s.Courses, "、"), ".")
}

func main() {
	var i MyInt = 15
	fmt.Println(i.ToHexStr()) // F

	i = 16
	fmt.Println(i.ToHexStr()) // 10

	ip := &i
	fmt.Println(ip.ToHexStr()) // 10

	fmt.Println("---------------- s1 --------------------")
	s1 := Student{}
	s1.SetName1("A")
	s1.SetCourses1([]string{"Math", "Chinese"}...)
	s1.Say1() //  ,I had learnt courses:   .

	s1.SetName1("A")
	s1.SetCourses1([]string{"Math", "Chinese"}...)
	s1.Say2() //  ,I had learnt courses:   .

	s1.SetName2("A")
	s1.SetCourses1([]string{"Math", "Chinese"}...)
	s1.Say1() // A ,I had learnt courses:   .

	s1.SetName2("A")
	s1.SetCourses2([]string{"Math", "Chinese"}...)
	s1.Say1() // A ,I had learnt courses:  Math、Chinese .

	s1.SetName2("A")
	s1.SetCourses2([]string{"Math", "Chinese"}...)
	s1.Say2() // A ,I had learnt courses:  Math、Chinese .

	fmt.Println("---------------- s2 --------------------")
	s2 := &Student{}
	s2.SetName1("B")
	s2.SetCourses1([]string{"Math", "Chinese"}...)
	s2.Say1() //  ,I had learnt courses:   .

	s2.SetName1("B")
	s2.SetCourses1([]string{"Math", "Chinese"}...)
	s2.Say2() //  ,I had learnt courses:   .

	s2.SetName2("B")
	s2.SetCourses1([]string{"Math", "Chinese"}...)
	s2.Say1() // B ,I had learnt courses:   .

	s2.SetName2("B")
	s2.SetCourses2([]string{"Math", "Chinese"}...)
	s2.Say1() // B ,I had learnt courses:  Math、Chinese .

	s2.SetName2("B")
	s2.SetCourses2([]string{"Math", "Chinese"}...)
	s2.Say2() // B ,I had learnt courses:  Math、Chinese .

}
最后修改 March 10, 2024: 更新 (ddf4687)