golang之interface(介面)與 reflect 機制

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

一、概述

  什麼是interface,簡單的說,interface是一組method的組合,通過interface來定義對象的一組行為;

  interface類型定義了一組方法,如果某個對象實現了某個介面的所有方法,則此對象就實現了此介面;

  1 package main  2   3 import "fmt"  4   5 type Human struct {  6     name  string  7     age   int   8     phone string  9 } 10  11 type Student struct { 12     Human  //匿名函數 13     school string 14     loan   float32 15 } 16  17 type Employee struct { 18     Human 19     company string 20     money   float32 21 } 22  23 //Human對象實現Sayhi方法 24 func (h *Human) Sayhi() { 25     fmt.Printf("hi, I am %s you can call me on %s\n", h.name, h.phone) 26 } 27  28 //Human對象實現了Sing方法 29 func (h *Human) Sing(lyrics string) { 30     fmt.Println("lalala...", lyrics) 31 } 32  33 //Human對象實現了Guzzle方法 34 func (h *Human) Guzzle(beerStein string) { 35     fmt.Println("Guzzle Guzzle...", beerStein) 36 } 37  38 //Student 實現了BorrowMoney方法 39 func (s *Student) BorrowMoney(amount float32) { 40     s.loan += amount 41 } 42  43 //Empolyee 重載了Human的Sayhi的方法 44 func (e *Employee) Sayhi() { 45     fmt.Printf("hi, I am %s, I work at %s. call me on %s\n", e.name, e.company, e.phone) 46 } 47  48 //Employee實現了SpendSalary的方法 49 func (e *Employee) SpendSalary(amount float32) { 50     e.money -= amount 51 } 52  53 //define interface 54 /* 55 type Men interface { 56     Sayhi() 57     Sing(lyrics string) 58     Guzzle(beerStein string) 59 } 60  61 type YoungChap interface { 62     Sayhi() 63     Sing(song string) 64     BorrowMoney(amount float32) 65 } 66  67 type ElderlyGent interface { 68     Sayhi() 69     Sing(song string) 70     SpendSalary(amount float32) 71 } 72 */ 73  74 //interface Men被Human,Student, Employee都實現 75 // Student, Employee包含了Human匿名欄位,所有也包含了其介面實現 76 type Men interface { 77     Sayhi() 78     Sing(lyrics string) 79 } 80  81 func main() { 82     mike := Student{Human{"Mike", 24, "22-22-xx"}, "MIT", 0.00} 83     paul := Student{Human{"paul", 26, "23-32-xx"}, "Harvard", 5.00} 84     sam := Employee{Human{"Sam", 46, "33-33-33"}, "Gling inc", 1000} 85     Tom := Employee{Human{"Tom", 33, "33-334-11"}, "Things ltd", 400} 86  87     var i Men //interface type 88     i = &mike 89     fmt.Println("this is Mike, a Student\n") 90     i.Sayhi() 91     i.Sing("my name is Mike") 92  93     i = &Tom 94     fmt.Println("this is Tom, an employee\n") 95     i.Sayhi() 96     i.Sing("my name is Tom") 97  98     x := make([]Men, 3) 99     x[0], x[1], x[2] = &paul, &sam, &mike100     for _, value := range x {101         value.Sayhi()102     }103 }

二、反射機制

 1 package main 2  3 import ( 4     "fmt" 5     "reflect" 6 ) 7  8 func main() { 9     var x float64 = 3.410     p := reflect.ValueOf(&x)11     v := p.Elem()12     fmt.Println(v)13     v.SetFloat(8.3)14     fmt.Println(v)15 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.