go語言學習筆記20------面向對象③

1.接口

1.1接口實現

package main

import "fmt"

type Human interface {
   sayhi()
}
type student struct {
   name string
   id   int
}

func (s *student) sayhi() {
   fmt.Println(s.name, s.id)
}

type teacher struct {
   name string
   id   int
}

func (t *teacher) sayhi() {
   fmt.Println(t.name, t.id)
}
func main() {
   var i Human
   var s student = student{"lili", 1}
   i = &s
   i.sayhi()

   var t teacher = teacher{"bobo", 2}
   i=&t
   i.sayhi()
}

//輸出結果
//lili 1
//bobo 2

1.2多態

接口作用實現多態。

package main

import "fmt"

type Human interface {
   sayhi()
}
type student struct {
   name string
   id   int
}

func (s *student) sayhi() {
   fmt.Println("654321",s.name, s.id)
}

type teacher struct {
   name string
   id   int
}

func (t *teacher) sayhi() {
   fmt.Println("123456",t.name, t.id)
}
func who(i Human){
   i.sayhi()
}
func main() {
   var s student = student{"lili", 1}
   who(&s)
   var t teacher = teacher{"bobo", 2}
   who(&t)
}

//輸出結果
//654321 lili 1
//123456 bobo 2

一個函數who可以調用不同的兩種方法。

1.3接口實例

接下來讓我們用一個計算機的實例來介看看接口的用處!
1)使用面向對象的思想實現一個加減功能的計算器

package main

import "fmt"

type opertion struct {

}

func (o *opertion) getresult(info string, num1 float64, num2 float64) (result float64) {
   switch info {
   case "+":
      result = num1 + num2
   case "-":
      result = num1 - num2
   }
   return
}
func main() {
    var a opertion
    result1:=a.getresult("+",2,4)
    fmt.Println(result1)
    return2:=a.getresult("-",5,2)
    fmt.Println(return2)
}
//輸出結果
//6
//3

我們定義了一個類(結構體),然後爲該類創建了一個方法,封裝了整個計算器功能,以後要使用直接使用該類(結構體)創建對象就可以了。這就是面向對象總的封裝性。
2)用接口實現

package main

import "fmt"

type opertion struct {
   num1 float64
   num2 float64
}
type getresult interface {
   getresult()float64
}

type Add struct {
   opertion
}

func (a *Add) getresult()  float64 {
   return a.num1+a.num2
}
type Sub struct{
   opertion
}
func (s *Sub) getresult () float64{
   return s.num1-s.num2
}
type factory struct {
}
func oper(i getresult)float64{
   return i.getresult()
}
func(f *factory)createfaction(info string,num1 float64,num2 float64)float64 {
   var result float64
   switch info {
   case "+":
      add := &Add{opertion{num1, num2}}
      result=oper(add)
   case "-":
      sub := &Sub{opertion{num1, num2}}
      result=oper(sub)
   }
   return result
}
func main() {
    var factory factory
    s:=factory.createfaction("+",10,20)
    fmt.Println(s)
    a:=factory.createfaction("-",30,20)
    fmt.Println(a)
}
//輸出結果
//30
//10

1.4接口繼承與轉換

package main

import "fmt"

type Humaner interface {
   sayhi()
}
type Person interface {
   Humaner
   sing(s string)
}
type student struct {
   name string
   id int
}

func (s *student)sayhi()  {
   fmt.Println("hello")
}
func (sa *student)sing(s string)  {
   fmt.Println("student 在唱着",s)
}
func main() {
 var i Person
 s:=&student{"lili",11}
 i=s
 i.sayhi()
 i.sing("哥哥")
}
//輸出結果
//hello
//student 在唱着 哥哥

接口繼承後,可以實現“超集”接口轉換“子集”接口,代碼如下:

package main

import "fmt"

type Humaner interface {
   sayhi()
}
type Person interface {
   Humaner
   sing(s string)
}
type student struct {
   name string
   id   int
}

func (s *student) sayhi() {
   fmt.Println("hello")
}
func (sa *student) sing(s string) {
   fmt.Println("student 在唱着", s)
}
func main() {
   var i Person
   s := &student{"lili", 11}
   i = s
   var m Humaner
//i=m   //err
   m=s
   m.sayhi()
   i.sing("哥哥")

}
//輸出結果
//hello
//student 在唱着 哥哥

1.5空接口

空接口(interface{})不包含任何的方法,正因爲如此,所有的類型都實現了空接口,因此空接口可以存儲任意類型的數值。
例如:

package main

import "fmt"

func main() {
   var i interface{}=1
   fmt.Println("i=",i)
   i="abc"
   fmt.Println("i=",i)
}
//輸出結果
//i= 1
//i= abc
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章