教程集 www.jiaochengji.com
教程集 >  Golang编程  >  正文 Go语言入门(十一) 接口编程

Go语言入门(十一) 接口编程

发布时间:2021-12-10   编辑:jiaochengji.com
教程集为您提供Go语言入门(十一) 接口编程等资源,欢迎您收藏本站,我们将为您提供最新的Go语言入门(十一) 接口编程资源
<h1>接口</h1> <h2>接口的定义</h2> <ul><li>

接口定义了一个对象化的行为规范

<ul><li>只定义规范,不实现</li> <li>具体的对象需要实现规范的细节</li> </ul></li> <li>实践 <ul><li>type 定义接口 interface</li> <li>接口里面是一组方法签名的集合</li> </ul></li> </ul><pre><code class="lang-go hljs">type Animal interface { Talk() Eat() Run() }</code></code></pre> <ul><li>实现 <ul><li>一个对象只要包含接口中的方法,那么就实现了这个接口</li> <li>接口类型的变量可以保存实现该接口的任何类型的实例</li> </ul></li> </ul><pre><code class="lang-go hljs">type Animal interface { Talk() Eat() Run() } type Dog struct { name string } //一个对象只要包含接口中的方法,那么就实现了这个接口 func (d *Dog) Eat() { fmt.Printf("%s is eating\n",d.name) } func (d *Dog) Talk() { fmt.Printf("%s is talking\n",d.name) } func (d *Dog) Run() { fmt.Printf("%s is running\n",d.name) } func main() { var dog = &Dog{ name: "旺财", } var a Animal //接口类型的变量可以保存实现该接口的任何类型的实例 a = dog a.Eat() a.Run() a.Talk() fmt.Printf("a:%v,dog:%v\n",a,dog) }</code></code></pre> <h3>接口实例</h3> <ul><li>计算公司所有职员的薪水</li> <li>每个职员的计算方式不同</li> </ul><h4>结构体实现</h4> <pre><code class="lang-go hljs">type Developer struct { Name string Base float64 } func (d *Developer)Calc() float64{ return d.Base } type PM struct { Name string Base float64 Option float64 } func (p *PM)Calc() float64{ return p.Base p.Option } type YY struct { Name string Base float64 Ratio float64 Option float64 } func (y *YY)Calc() float64 { return y.Base y.Option * y.Ratio } type EmployeeMgr struct { devlist []*Developer pmlist []*PM yylist []*YY } func (e *EmployeeMgr)Calc() float64 { var sum float64 for _,v := range e.devlist { sum = v.Calc() } for _,v1 := range e.pmlist { sum = v1.Calc() } for _,v2 := range e.yylist { sum = v2.Calc() } return sum } func (e *EmployeeMgr)AddDev(d *Developer) { e.devlist = append(e.devlist, d) } func (e *EmployeeMgr)AddPM(p *PM) { e.pmlist = append(e.pmlist, p) } func (e *EmployeeMgr)AddYY(y *YY) { e.yylist = append(e.yylist, y) } func main() { var e = &EmployeeMgr{} dev := &Developer{ Name: "develop", Base: 5000, } e.AddDev(dev) pm := &PM{ Name: "pm", Base: 10000, Option: 2000, } e.AddPM(pm) yy := &YY{ Name: "yy", Base: 23000, Option: 9000, Ratio: 0.67, } e.AddYY(yy) sum := e.Calc() fmt.Printf("sum:%.2f\n",sum) }</code></code></pre> <h4>接口实现</h4> <pre><code class="lang-go hljs"> type Employee interface { Calc() float64 //接口返回 } type Developer struct { Name string Base float64 } func (d *Developer)Calc() float64{ return d.Base } type PM struct { Name string Base float64 Option float64 } func (p *PM)Calc() float64{ return p.Base p.Option } type YY struct { Name string Base float64 Ratio float64 Option float64 } func (y *YY)Calc() float64 { return y.Base y.Option * y.Ratio } type EmployeeMgr struct { employeelist []Employee } func (e *EmployeeMgr) Calc() float64 { var sum float64 for _,v := range e.employeelist { sum = v.Calc() } return sum } func (e *EmployeeMgr) AddEmployee(d Employee) { e.employeelist = append(e.employeelist,d) } func main() { var e = &EmployeeMgr{} dev := &Developer{ Name: "develop", Base: 5000, } e.AddEmployee(dev) pm := &PM{ Name: "pm", Base: 10000, Option: 2000, } e.AddEmployee(pm) yy := &YY{ Name: "yy", Base: 23000, Option: 9000, Ratio: 0.67, } e.AddEmployee(yy) sum := e.Calc() fmt.Printf("sum:%.2f\n",sum) }</code></code></pre> <h3>空接口</h3> <ul><li>空接口没有定义任何方法</li> <li>所以了任何类型都实现了空接口</li> </ul><pre><code class="lang-go hljs">func main() { var a interface{} var b int a = b fmt.Printf("a=%v,a:%T\n",a,a) var c float64 a = c fmt.Printf("a=%v,a:%T\n",a,a) }</code></code></pre> <h3>类型断言</h3> <ul><li>需要引入v,ok := i(T)机制</li> </ul><pre><code class="lang-go hljs">func Describe(a Animal) { /*代码有坑 dog := a.(*Dog) dog.Eat() */ dog,ok := a.(*Dog) if !ok { fmt.Printf("convert to dog error\n") return } fmt.Printf("describe dog succ\n") dog.Run() fmt.Printf("describe dog succ------\n") }</code></code></pre> <ul><li>类型断言</li> </ul><pre><code class="lang-go hljs">func DescribeSwitch(a Animal) { fmt.Printf("DescribeSwitch(a) Begin\n") switch a.(type) { case *Dog: dog := a.(*Dog) dog.Run() case *Pig: pig := a.(*Pig) pig.Eat() } fmt.Printf("DescribeSwitch(a) End\n") }</code></code></pre> <ul><li>类型断言改进版</li> </ul><pre><code class="lang-go hljs">func DescribeSwitch(a Animal) { fmt.Printf("DescribeSwitch(a) Begin\n") switch v:=a.(type) { case *Dog: dog := v dog.Run() case *Pig: pig := v pig.Eat() } fmt.Printf("DescribeSwitch(a) End\n") }</code></code></pre> <ul><li>指针接收</li> <li>同一个类型可以实现多个接口</li> </ul><pre><code class="lang-go hljs">type Animal interface { Talk() Eat() Run() } type BuRuAnimal interface { ChiNai() } type Dog struct { name string } //一个对象只要包含接口中的方法,那么就实现了这个接口 func (d Dog) Eat() { fmt.Printf("%s is eating\n",d.name) } func (d Dog) Talk() { fmt.Printf("%s is talking\n",d.name) } func (d Dog) Run() { fmt.Printf("%s is running\n",d.name) } func (d Dog) ChiNai() { fmt.Printf("%s is chinai\n",d.name) } func main() { var dog = &Dog{ name: "旺财", } var a Animal //接口类型的变量可以保存实现该接口的任何类型的实例 a = dog //Describe(a) //DescribeSwitch(a) a.Eat() var dogVar = Dog{name:"来福"} a = dogVar a.Run() //实现多个接口 var b BuRuAnimal b = dog b.ChiNai() }</code></code></pre> <ul><li>接口嵌套</li> </ul><pre><code class="lang-go hljs">type Animal interface { Talk() Eat() Run() } type BuRuAnimal interface { ChiNai() } type AdvanceAnimal interface { Animal BuRuAnimal } type Dog struct { name string } //一个对象只要包含接口中的方法,那么就实现了这个接口 func (d Dog) Eat() { fmt.Printf("%s is eating\n",d.name) } func (d Dog) Talk() { fmt.Printf("%s is talking\n",d.name) } func (d Dog) Run() { fmt.Printf("%s is running\n",d.name) } func (d Dog) ChiNai() { fmt.Printf("%s is chinai\n",d.name) } func main() { var dog = &Dog{ name: "旺财", } var a AdvanceAnimal a = dog a.ChiNai() }</code></code></pre> <h2>接口实例详解</h2> <ul><li>io包中的writer接口</li> </ul><pre><code class="lang-go hljs">type Test struct { data string } func (t *Test)Write(p []byte) (n int,err error) { t.data = string(p) return len(p),nil } func FprintfDemo() { file,_ := os.Create("./a.txt") fmt.Fprintf(os.Stdout,"hello world\n") fmt.Fprintf(file,"hello World\n") //终端文件写入 var t *Test = &Test{} //将字符串写入内存,然后再读取 fmt.Fprintf(t,"this is a test interface %s","dasdada") fmt.Printf("t.data=%s\n",t.data) }</code></code></pre> <ul><li>fmt包中的Stringer接口
返回字符串</li> </ul><pre><code class="lang-go hljs">type Student struct { Name string Age int } func (s *Student)String()string { data,_ := json.Marshal(s) return string(data) } func main() { var s = &Student{ Name: "ABC", Age: 22, } fmt.Printf("s = %v\n",s) }</code></code></pre> <ul><li>error接口</li> </ul><pre><code class="lang-go hljs">type MyError struct { When time.Time What string } func (e *MyError) Error()string{ str:= fmt.Sprintf("time=%v,message=%s\n",e.When,e.What) fmt.Printf("1:%T\n",str) return str } func run()error { fmt.Printf("0\n") str:=MyError{time.Now(),"it did not work well"} fmt.Printf("2:%T\n",str) fmt.Printf("%v,%s\n",time.Now(),"error to run!!") return &str } func main() { if err := run();err!=nil { fmt.Printf("3:%T\n",err) fmt.Println(err) } }</code></code></pre>
到此这篇关于“Go语言入门(十一) 接口编程”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
想系统学习GO语言(Golang
Go语言发展历史、核心、特性及学习路线
Go 语言到底适合干什么?
php入门教程(索引)
Go 语言十年而立,Go2 蓄势待发
Go语言的主要特性和发展影响
Go语言学习3----Go语言特色
go run main.go 参数_Go语言入门:Hello world
兄弟连golang神技(1)-关于 Go 语言的介绍
2018年最全Go语言教程零基础入门到进阶实战视频

[关闭]
~ ~