[Golang] Interfaces & polymorphism 接口和多型

Golang Interfaces & polymorphism 接口和多型

  • Golang的 Interface
    • 首先要知道 a value can be more then one type
    • a value can be more them one type
    • a value can be more them one type
  • 第一步先創建type person和type secretAgent
    • type person struct {
      	first string
      	last  string
      }
      
      type secretAgent struct {
      	person
      	ltk bool
      }
      
      

       

  • 然後分別 attach method speak 到 type person 和 type secretAgent
    • 
      func (s secretAgent) speak() {
      	fmt.Println("I am", s.first, s.last, " - the secretAgent speak")
      }
      
      func (p person) speak() {
      	fmt.Println("I and", p.first, p.last, " - the secretAgent speak")
      }

       

  • 接著,創建 interface human
    • type human interface {
      	// If you have this method then you are my type
      	// define which method a type must have to implement interface
      	speak()
      }

       

  • 接口(interface)是在定義 type 必須實作哪些 method,才算是這個 type interface
    • 這邊的 type person 和 type secretAgent,都擁有 attach 的 speak method,所以符合 type human interface 的定義
    • 那 type person 和 type secretAgent,就也同時是 type human 和 type person
  • 完整範例程式碼:
    package main
    
    import "fmt"
    
    type person struct {
    	first string
    	last  string
    }
    
    type secretAgent struct {
    	person
    	ltk bool
    }
    
    func (s secretAgent) speak() {
    	fmt.Println("I am", s.first, s.last, " - the secretAgent speak")
    }
    
    func (p person) speak() {
    	fmt.Println("I and", p.first, p.last, " - the secretAgent speak")
    }
    
    // keyword identifier type
    // a value can be more then one type
    // a value can be more then one type
    
    type human interface {
    	// If you have this method then you are my type
    	// define which method a type must have to implement interface
    	speak()
    }
    
    func bar(h human) {
    	switch h.(type) {
    	case person:
    		fmt.Println("I was passed into barrrrrrr", h.(person).first)
    	case secretAgent:
    		fmt.Println("I was passed into barrrrrrr", h.(secretAgent).first)
    	}
    	fmt.Println("I was passed into bar", h)
    }
    
    type hotdog int
    
    func main() {
    	// sa1是type secretAgent,因為他有speak() method,所以sa1也同時是type human
    	sa1 := secretAgent{
    		person: person{
    			"James",
    			"Bond",
    		},
    		ltk: true,
    	}
    
    	sa2 := secretAgent{
    		person: person{
    			"Miss",
    			"Moneypenny",
    		},
    		ltk: true,
    	}
    
    	p1 := person{
    		first: "Dr.",
    		last:  "Yes",
    	}
    
    	fmt.Println(sa1)
    	fmt.Println(sa2)
    
    	fmt.Println(p1)
    
    	bar(sa1)
    	bar(sa2)
    	bar(p1)
    
    	// conversion
    	var x hotdog = 42
    	fmt.Println(x)
    	fmt.Printf("%T\n", x)
    	var y int
    	y = int(x)
    
    	fmt.Println(y)
    	fmt.Printf("%T\n", y)
    }
    

     

  • End
  • 參考資料:https://www.ardanlabs.com/blog/2015/09/composition-with-go.html