<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### **第一題:** 使用反射來遍歷結構體的字段,調用結構體的方法,并獲取結構體標簽的值 ![](https://img.kancloud.cn/ca/4a/ca4ac7901a4af8dfdfeb81c0347e5f51_1209x2057.png) ``` package main import( "fmt" "reflect" ) type Student struct { Name string `json:"name"` Age int `json:"age"` Sex string `json:"sex"` Skill string `json:"skill"` Score float64 `json:"score"` } func (stu Student) Print(){ fmt.Printf("stu value is %v\n",stu) } func (stu Student) GetSum(n,y int) int { res := n + y return res } func (stu Student)Set(Name,Sex,Skill string,Age int,Score float64){ stu.Name = Name stu.Age = Age stu.Skill = Skill stu.Sex = Sex stu.Score = Score } func TestStruct(a interface{}){ // 獲取a的Type aType := reflect.TypeOf(a) // 獲取a的value avalue := reflect.ValueOf(a) // 獲取avalue的類別 aKind := avalue.Kind() if aKind != reflect.Struct{ fmt.Println("expext struct") return } // 獲取結構體有幾個字段 stunum := avalue.NumField() // 返回v持有結構體類型值的字段數,如果v的Kind不是Struct會panic fmt.Printf("Student field number is %d",stunum) // 遍歷結構體所有字段 for i := 0;i < stunum;i++{ // 獲取到struct標簽,注意需要通過reflect.Type來獲取tag標簽的值 tagval := aType.Field(i).Tag.Get("json") // 判斷字段有tag就顯示,沒有就不顯示 if tagval != ""{ fmt.Printf("tag How many %d tag = %v\n",i,tagval) } } // 獲取到改結構體有多少方法 mehtodall := avalue.NumMethod() // 返回v持有值的方法集的方法數目 fmt.Printf("avalue method by %d\n",mehtodall) /** var params []refkect.Value 方法排序是默認按照,函數名排序(ASCII)碼 返回v持有值類型的第i個方法的已綁定(到v的持有值的)狀態的函數形式的Value封裝 返回值調用Call方法時不應包含接受者;返回持有值的函數總是使用v的持有者作為接受者(即第一個參數) 如果i出界,或者v的持有者是接口類型的零值(nil),會包panic **/ avalue.Method(1).Call(nil) // Call方法使用輸入的參數in調用v的持有函數 // 聲明了,reflect.Value var params []reflect.Value params = append(params,reflect.ValueOf(10)) params = append(params,reflect.ValueOf(20)) res := avalue.Method(0).Call(params) // 傳入的參數是[]reflect.Value,返回[]reflect.Value fmt.Println("res = ",res[0].Int()) } func main(){ per := Student{ Name: "風清揚", Age: 70, Sex: "男", Skill: "獨孤九劍", Score: 98.9988, } TestStruct(per) } ``` <br> <br> **運行結果** ``` Student field number is 5tag How many 0 tag = name tag How many 1 tag = age tag How many 2 tag = sex tag How many 3 tag = skill tag How many 4 tag = score avalue method by 3 stu value is {風清揚 70 男 獨孤九劍 98.9988} res = 30 ``` <br> **如上代碼說明:** (一)34行Kind:Kind返回v持有的值的分類,如果v是Value零值,返回值為Invalid ![](https://img.kancloud.cn/65/35/65352b5d6840d07ed592b06e7e059359_846x129.png) <br> <br> (二)41行NumField:就是將結構體的字段數量返回,reflect.value下的方法 ![](https://img.kancloud.cn/9a/03/9a035f6261f868656d96cd5ddac34f88_904x135.png) <br> <br> (三)48行Field:就是將將結構體的第n個字段返回 ![](https://img.kancloud.cn/14/59/1459bfa63f770c6148d38e3399d58b70_860x135.png) <br> <br> (四)48行Field:改Field是Type的Field,也就是標簽, ![](https://img.kancloud.cn/71/0e/710e319cf1ea5b65d42c2590c964987c_922x328.png) <br> <br> (五)56行NumMethod:會統計Student這個結構體中有多少個方法 ![](https://img.kancloud.cn/59/a6/59a653e6499749fabbab260b1a8c3264_832x128.png) <br> <br> (六)68行Method:改方法在value函數封裝,所以,Method要傳入結構體綁定的第幾個方法進行調用,調用形式,也就是傳入1,底層會按照方法名,進行ASCII進行順序調用 返回v持有值類型的第i個方法的已綁定(到v的持有值的)狀態的函數形式的Value封裝 ![](https://img.kancloud.cn/ab/fc/abfc6255989437180c9ef35831fffdcf_869x169.png) <br> <br> (七)62行Call:如果method調用的方法,不需要傳參則Call(nil)即可 ![](https://img.kancloud.cn/e2/74/e274b5bc83280b6f5d34fe6d17e56595_841x192.png) <br> <br> ### **第二題:** 1)使用反射的方式來獲取結構體的tag標簽,遍歷字段的值,修改字段值,調用結構體方法(要求:通過傳遞地址的方式完成在前面案例上修改即可)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看