{"rsdb":{"rid":"397312","subhead":"","postdate":"0","aid":"273517","fid":"78","uid":"1","topic":"1","content":"
\n

\u5341\u4e09\u3001\u5bf9\u8c61<\/h1> \n

9.\u630e\u5305\u521b\u5efa\u7ed3\u6784\u4f53\u5b9e\u4f8b<\/h2> \n

\u30101\u3011\u521b\u5efa\u4e0d\u540c\u7684\u5305\uff1a<\/p> \n

\"image-20230908174309371\"<\/p> \n

\u30102\u3011student.go:<\/p> \n \"image-20230908174316139\" \n

\u30103\u3011main.go:<\/p> \n \"image-20230908174320728\" \n

\u53d1\u73b0\uff1a\u5982\u679c\u7ed3\u6784\u4f53\u9996\u5b57\u6bcd\u5927\u5199\u7684\u8bdd\uff0c\u5728\u5176\u5b83\u5305\u4e0b\u53ef\u4ee5\u8bbf\u95ee
\u4f46\u662f\uff1a\u5982\u679c\u7ed3\u6784\u4f53\u7684\u9996\u5b57\u6bcd\u5c0f\u5199\uff1f<\/p> \n

\"image-20230908174328206\"<\/p> \n

\u89e3\u51b3\uff1a\u7ed3\u6784\u4f53\u9996\u5b57\u6bcd\u5c0f\u5199\uff0c\u8de8\u5305\u8bbf\u95ee\u6ca1\u95ee\u9898\uff1a---\u300b\u5de5\u5382\u6a21\u5f0f<\/p> \n \"image-20230908174332174\" \n \"image-20230908174336015\" \n

10.\u5c01\u88c5<\/h2> \n

\u30101\u3011\u4ec0\u4e48\u662f\u5c01\u88c5\uff1a
\u5c01\u88c5(encapsulation)\u5c31\u662f\u628a\u62bd\u8c61\u51fa\u7684\u5b57\u6bb5\u548c\u5bf9\u5b57\u6bb5\u7684\u64cd\u4f5c\u5c01\u88c5\u5728\u4e00\u8d77\uff0c\u6570\u636e\u88ab\u4fdd\u62a4\u5728\u5185\u90e8,\u7a0b\u5e8f\u7684\u5176\u5b83\u5305\u53ea\u6709\u901a\u8fc7\u88ab\u6388\u6743\u7684\u64cd\u4f5c\u65b9\u6cd5\uff0c\u624d\u80fd\u5bf9\u5b57\u6bb5\u8fdb\u884c\u64cd\u4f5c\u3002<\/p> \n

\u30102\u3011\u5c01\u88c5\u7684\u597d\u5904\uff1a<\/p> \n

    \n
  1. \u9690\u85cf\u5b9e\u73b0\u7ec6\u8282<\/li> \n
  2. \u63d0\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u9a8c\u8bc1\uff0c\u4fdd\u8bc1\u5b89\u5168\u5408\u7406<\/li> \n <\/ol> \n

    \u30103\u3011Golang\u4e2d\u5982\u4f55\u5b9e\u73b0\u5c01\u88c5\uff1a<\/p> \n

      \n
    1. \u5efa\u8bae\u5c06\u7ed3\u6784\u4f53\u3001\u5b57\u6bb5(\u5c5e\u6027)\u7684\u9996\u5b57\u6bcd\u5c0f\u5199(\u5176\u5b83\u5305\u4e0d\u80fd\u4f7f\u7528\uff0c\u7c7b\u4f3cprivate\uff0c\u5b9e\u9645\u5f00\u53d1\u4e0d\u5c0f\u5199\u4e5f\u53ef\u80fd\uff0c\u56e0\u4e3a\u5c01\u88c5\u6ca1\u6709\u90a3\u4e48\u4e25\u683c)<\/li> \n
    2. \u7ed9\u7ed3\u6784\u4f53\u6240\u5728\u5305\u63d0\u4f9b\u4e00\u4e2a\u5de5\u5382\u6a21\u5f0f\u7684\u51fd\u6570\uff0c\u9996\u5b57\u6bcd\u5927\u5199\uff08\u7c7b\u4f3c\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff09<\/li> \n
    3. \u63d0\u4f9b\u4e00\u4e2a\u9996\u5b57\u6bcd\u5927\u5199\u7684Set\u65b9\u6cd5(\u7c7b\u4f3c\u5176\u5b83\u8bed\u8a00\u7684public)\uff0c\u7528\u4e8e\u5bf9\u5c5e\u6027\u5224\u65ad\u5e76\u8d4b\u503c
      func (var \u7ed3\u6784\u4f53\u7c7b\u578b\u540d)SetXxx(\u53c2\u6570\u5217\u8868){
      \/\/\u52a0\u5165\u6570\u636e\u9a8c\u8bc1\u7684\u4e1a\u52a1\u903b\u8f91
      var.Age =\u53c2\u6570
      }<\/li> \n
    4. \u63d0\u4f9b\u4e00\u4e2a\u9996\u5b57\u6bcd\u5927\u5199\u7684Get\u65b9\u6cd5(\u7c7b\u4f3c\u5176\u5b83\u8bed\u8a00\u7684public)\uff0c\u7528\u4e8e\u83b7\u53d6\u5c5e\u6027\u7684\u503c
      func (var\u7ed3\u6784\u4f53\u7c7b\u578b\u540d) GetXxx() (\u8fd4\u56de\u503c\u5217\u8868){
      return var.\u5b57\u6bb5;
      }
      \u30104\u3011\u4ee3\u7801\u5b9e\u73b0\uff1a<\/li> \n <\/ol> \n \"image-20230908174353631\" \n \"image-20230908174356625\" \n \"image-20230908174402616\" \n

      11.\u7ee7\u627f<\/h2> \n

      \u30101\u3011\u7ee7\u627f\u7684\u5f15\u5165\uff1a
      \u5f53\u591a\u4e2a\u7ed3\u6784\u4f53\u5b58\u5728\u76f8\u540c\u7684\u5c5e\u6027(\u5b57\u6bb5)\u548c\u65b9\u6cd5\u65f6,\u53ef\u4ee5\u4ece\u8fd9\u4e9b\u7ed3\u6784\u4f53\u4e2d\u62bd\u8c61\u51fa\u7ed3\u6784\u4f53,\u5728\u8be5\u7ed3\u6784\u4f53\u4e2d\u5b9a\u4e49\u8fd9\u4e9b\u76f8\u540c\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u5176\u5b83\u7684\u7ed3\u6784\u4f53\u4e0d\u9700\u8981\u91cd\u65b0\u5b9a\u4e49\u8fd9\u4e9b\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u53ea\u9700\u5d4c\u5957\u4e00\u4e2a\u533f\u540d\u7ed3\u6784\u4f53\u5373\u53ef\u3002\u4e5f\u5c31\u662f\u8bf4:\u5728Golang\u4e2d\uff0c\u5982\u679c\u4e00\u4e2astruct\u5d4c\u5957\u4e86\u53e6\u4e00\u4e2a\u533f\u540d\u7ed3\u6784\u4f53\uff0c\u90a3\u4e48\u8fd9\u4e2a\u7ed3\u6784\u4f53\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u533f\u540d\u7ed3\u6784\u4f53\u7684\u5b57\u6bb5\u548c\u65b9\u6cd5\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u7ee7\u627f\u7279\u6027\u3002<\/p> \n \"image-20230908174440091\" \n

      \u30102\u3011\u4ee3\u7801\u5f15\u5165\uff1a<\/p> \n

      package main\nimport (\n        "fmt"\n)\n\/\/\u5b9a\u4e49\u52a8\u7269\u7ed3\u6784\u4f53\uff1a\ntype Animal struct{\n        Age int\n        Weight float32\n}\n\/\/\u7ed9Animal\u7ed1\u5b9a\u65b9\u6cd5\uff1a\u558a\u53eb\uff1a\nfunc (an *Animal) Shout(){\n        fmt.Println("\u6211\u53ef\u4ee5\u5927\u58f0\u558a\u53eb")\n}\n\/\/\/\/\u7ed9Animal\u7ed1\u5b9a\u65b9\u6cd5\uff1a\u81ea\u6211\u5c55\u793a\uff1a\nfunc (an *Animal) ShowInfo(){\n        fmt.Printf("\u52a8\u7269\u7684\u5e74\u9f84\u662f\uff1a%v,\u52a8\u7269\u7684\u4f53\u91cd\u662f\uff1a%v",an.Age,an.Weight)\n}\n\/\/\u5b9a\u4e49\u7ed3\u6784\u4f53\uff1aCat\ntype Cat struct{\n        \/\/\u4e3a\u4e86\u590d\u7528\u6027\uff0c\u4f53\u73b0\u7ee7\u627f\u601d\u7ef4\uff0c\u5d4c\u5165\u533f\u540d\u7ed3\u6784\u4f53\uff1a\u2014\u2014\u300b\u5c06Animal\u4e2d\u7684\u5b57\u6bb5\u548c\u65b9\u6cd5\u90fd\u8fbe\u5230\u590d\u7528\n        Animal\n}\n\/\/\u5bf9Cat\u7ed1\u5b9a\u7279\u6709\u7684\u65b9\u6cd5\uff1a\nfunc (c *Cat) scratch(){\n        fmt.Println("\u6211\u662f\u5c0f\u732b\uff0c\u6211\u53ef\u4ee5\u6320\u4eba")\n}\nfunc main(){\n        \/\/\u521b\u5efaCat\u7ed3\u6784\u4f53\u793a\u4f8b\uff1a\n        cat := &Cat{}\n        cat.Animal.Age = 3\n        cat.Animal.Weight = 10.6\n        cat.Animal.Shout()\n        cat.Animal.ShowInfo()\n        cat.scratch()\n}\n\n\n<\/code><\/pre> \n 

      \u30103\u3011\u7ee7\u627f\u7684\u4f18\u70b9\uff1a
      \u63d0\u9ad8\u4ee3\u7801\u7684\u590d\u7528\u6027\u3001\u6269\u5c55\u6027<\/p> \n

      12.\u7ee7\u627f\u7684\u6ce8\u610f\u4e8b\u9879<\/h2> \n

      \u30101\u3011\u7ed3\u6784\u4f53\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u533f\u540d\u7ed3\u6784\u4f53\u6240\u6709\u7684\u5b57\u6bb5\u548c\u65b9\u6cd5\uff0c\u5373:\u9996\u5b57\u6bcd\u5927\u5199\u6216\u8005\u5c0f\u5199\u7684\u5b57\u6bb5\u3001\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u4f7f\u7528\u3002<\/p> \n

       package main\nimport (\n        "fmt"\n)\n\/\/\u5b9a\u4e49\u52a8\u7269\u7ed3\u6784\u4f53\uff1a\ntype Animal struct{\n        Age int\n        weight float32\n}\n\/\/\u7ed9Animal\u7ed1\u5b9a\u65b9\u6cd5\uff1a\u558a\u53eb\uff1a\nfunc (an *Animal) Shout(){\n        fmt.Println("\u6211\u53ef\u4ee5\u5927\u58f0\u558a\u53eb")\n}\n\/\/\/\/\u7ed9Animal\u7ed1\u5b9a\u65b9\u6cd5\uff1a\u81ea\u6211\u5c55\u793a\uff1a\nfunc (an *Animal) showInfo(){\n        fmt.Printf("\u52a8\u7269\u7684\u5e74\u9f84\u662f\uff1a%v,\u52a8\u7269\u7684\u4f53\u91cd\u662f\uff1a%v",an.Age,an.weight)\n}\n\/\/\u5b9a\u4e49\u7ed3\u6784\u4f53\uff1aCat\ntype Cat struct{\n        \/\/\u4e3a\u4e86\u590d\u7528\u6027\uff0c\u4f53\u73b0\u7ee7\u627f\u601d\u7ef4\uff0c\u5d4c\u5165\u533f\u540d\u7ed3\u6784\u4f53\uff1a\u2014\u2014\u300b\u5c06Animal\u4e2d\u7684\u5b57\u6bb5\u548c\u65b9\u6cd5\u90fd\u8fbe\u5230\u590d\u7528\n        Animal\n}\n\/\/\u5bf9Cat\u7ed1\u5b9a\u7279\u6709\u7684\u65b9\u6cd5\uff1a\nfunc (c *Cat) scratch(){\n        fmt.Println("\u6211\u662f\u5c0f\u732b\uff0c\u6211\u53ef\u4ee5\u6320\u4eba")\n}\nfunc main(){\n        \/\/\u521b\u5efaCat\u7ed3\u6784\u4f53\u793a\u4f8b\uff1a\n        cat := &Cat{}\n        cat.Animal.Age = 3\n        cat.Animal.weight = 10.6\n        cat.Animal.Shout()\n        cat.Animal.showInfo()\n        cat.scratch()\n}\n<\/code><\/pre> \n 

      \u30102\u3011\u533f\u540d\u7ed3\u6784\u4f53\u5b57\u6bb5\u8bbf\u95ee\u53ef\u4ee5\u7b80\u5316\u3002<\/p> \n \n

      \u7b49\u4ef7\u4e8e\uff1a<\/p> \n \"image-20230908174532157\" \n

      cat.Age --->cat\u5bf9\u5e94\u7684\u7ed3\u6784\u4f53\u4e2d\u627e\u662f\u5426\u6709Age\u5b57\u6bb5\uff0c\u5982\u679c\u6709\u76f4\u63a5\u4f7f\u7528\uff0c\u5982\u679c\u6ca1\u6709\u5c31\u53bb\u627e\u5d4c\u5165\u7684\u7ed3\u6784\u4f53\u7c7b\u578b\u4e2d\u7684Age
      \u30103\u3011\u5f53\u7ed3\u6784\u4f53\u548c\u533f\u540d\u7ed3\u6784\u4f53\u6709\u76f8\u540c\u7684\u5b57\u6bb5\u6216\u8005\u65b9\u6cd5\u65f6\uff0c\u7f16\u8bd1\u5668\u91c7\u7528\u5c31\u8fd1\u8bbf\u95ee\u539f\u5219\u8bbf\u95ee\uff0c\u5982\u5e0c\u671b\u8bbf\u95ee\u533f\u540d\u7ed3\u6784\u4f53\u7684\u5b57\u6bb5\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u533f\u540d\u7ed3\u6784\u4f53\u540d\u6765\u533a\u5206\u3002<\/p> \n

      package main\nimport (\n        "fmt"\n)\n\/\/\u5b9a\u4e49\u52a8\u7269\u7ed3\u6784\u4f53\uff1a\ntype Animal struct{\n        Age int\n        weight float32\n}\n\/\/\u7ed9Animal\u7ed1\u5b9a\u65b9\u6cd5\uff1a\u558a\u53eb\uff1a\nfunc (an *Animal) Shout(){\n        fmt.Println("\u6211\u53ef\u4ee5\u5927\u58f0\u558a\u53eb")\n}\n\/\/\/\/\u7ed9Animal\u7ed1\u5b9a\u65b9\u6cd5\uff1a\u81ea\u6211\u5c55\u793a\uff1a\nfunc (an *Animal) showInfo(){\n        fmt.Printf("\u52a8\u7269\u7684\u5e74\u9f84\u662f\uff1a%v,\u52a8\u7269\u7684\u4f53\u91cd\u662f\uff1a%v",an.Age,an.weight)\n}\n\/\/\u5b9a\u4e49\u7ed3\u6784\u4f53\uff1aCat\ntype Cat struct{\n        \/\/\u4e3a\u4e86\u590d\u7528\u6027\uff0c\u4f53\u73b0\u7ee7\u627f\u601d\u7ef4\uff0c\u5d4c\u5165\u533f\u540d\u7ed3\u6784\u4f53\uff1a\u2014\u2014\u300b\u5c06Animal\u4e2d\u7684\u5b57\u6bb5\u548c\u65b9\u6cd5\u90fd\u8fbe\u5230\u590d\u7528\n        Animal\n        Age int\n}\nfunc (c *Cat) showInfo(){\n        fmt.Printf("~~~~~~~~\u52a8\u7269\u7684\u5e74\u9f84\u662f\uff1a%v,\u52a8\u7269\u7684\u4f53\u91cd\u662f\uff1a%v",c.Age,c.weight)\n}\n\/\/\u5bf9Cat\u7ed1\u5b9a\u7279\u6709\u7684\u65b9\u6cd5\uff1a\nfunc (c *Cat) scratch(){\n        fmt.Println("\u6211\u662f\u5c0f\u732b\uff0c\u6211\u53ef\u4ee5\u6320\u4eba")\n}\nfunc main(){\n        \/\/\u521b\u5efaCat\u7ed3\u6784\u4f53\u793a\u4f8b\uff1a\n        \/\/ cat := &Cat{}\n        \/\/ cat.Age = 3\n        \/\/ cat.weight = 10.6\n        \/\/ cat.Shout()\n        \/\/ cat.showInfo()\n        \/\/ cat.scratch()\n        cat := &Cat{}\n        cat.weight = 9.4\n        cat.Age = 10 \/\/\u5c31\u8fd1\u539f\u5219\n        cat.Animal.Age = 20\n        cat.showInfo()\/\/\u5c31\u8fd1\u539f\u5219\n        cat.Animal.showInfo()\n}\n<\/code><\/pre> \n 

      \u30104\u3011Golang\u4e2d\u652f\u6301\u591a\u7ee7\u627f\uff1a\u5982\u4e00\u4e2a\u7ed3\u6784\u4f53\u5d4c\u5957\u4e86\u591a\u4e2a\u533f\u540d\u7ed3\u6784\u4f53\uff0c\u90a3\u4e48\u8be5\u7ed3\u6784\u4f53\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5d4c\u5957\u7684\u533f\u540d\u7ed3\u6784\u4f53\u7684\u5b57\u6bb5\u548c\u65b9\u6cd5\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u591a\u91cd\u7ee7\u627f\u3002\u4e3a\u4e86\u4fdd\u8bc1\u4ee3\u7801\u7684\u7b80","orderid":"0","title":"Go\u5b66\u4e60\u7b14\u8bb04(\u4e00)","smalltitle":"","mid":"0","fname":"GO","special_id":"0","bak_id":"0","info":"0","hits":"508","pages":"6","comments":"0","posttime":"2023-09-23 15:44:03","list":"1695455043","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"\u4e60\u7b14\u8bb0<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"112.94.1.100","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Go\u5b66\u4e60\u7b14\u8bb04","lastview":"1716081969","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}