golang接口的实现以及注意事项。
发布时间:2020-12-16 18:59:52 所属栏目:大数据 来源:网络整理
导读:package mainimport ("fmt")type Animal interface {move()}type Human struct {i int}func (r Human) move() { // 注意fmt.Println("人类行走")r.i++}type Bird struct {i int}func (r *Bird) move() {fmt.Println("鸟类行走")r.i++}func moveTest1(animal
package main import ( "fmt" ) type Animal interface { move() } type Human struct { i int } func (r Human) move() { // 注意 fmt.Println("人类行走") r.i++ } type Bird struct { i int } func (r *Bird) move() { fmt.Println("鸟类行走") r.i++ } func moveTest1(animal Animal) { animal.move() } // 虽然这个函数的定义没有错误,但实际上完全用不上! // 因为指向接口的指针,在接口实现类中,是无法转换的 func moveTest2(animal *Animal) { (*animal).move() } func main() { h1 := Human{0} moveTest1(h1) moveTest1(h1) moveTest1(h1) fmt.Println(h1.i) fmt.Println("----------------") h2 := &Human{0} moveTest1(h2) moveTest1(h2) moveTest1(h2) fmt.Println(h2.i) fmt.Println("----------------") // h3 := Human{0} // moveTest2(h3) // moveTest2(h3) // moveTest2(h3) // fmt.Println(h3.i) // // fmt.Println("----------------") // b1 := Bird{0} // moveTest1(b1) // moveTest1(b1) // moveTest1(b1) // fmt.Println(b1.i) // // fmt.Println("----------------") b2 := &Bird{0} moveTest1(b2) moveTest1(b2) moveTest1(b2) fmt.Println(b2.i) fmt.Println("----------------") // b3 := &Bird{0} // moveTest2(b3) // moveTest2(b3) // moveTest2(b3) // fmt.Println(b3.i) // // fmt.Println("----------------") }
人类行走
以上用了一个简单说明了golang在使用中需要注意的地方。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |