本篇主要分享go语言的面向对象编程小实例,加深对go语言面向对象编程部分内容认识。
类
在 Go 语言中没有类(Class)的概念,Go 语言中可以使用结构体(Structs)对属性进行封装,结构体就像是类的一种简化形式。
方法
Go 语言中方法(Methods):Go 方法是作用在接收者(receiver)上的一个函数,接收者是某种类型的变量。因此方法是一种特殊类型的函数。
继承
在 Go 语言中没有 extends 关键字,它使用在结构体中内嵌匿名类型的方法来实现继承。
package main import ( "fmt" ) type Person struct {//定义属性内容 name string sex byte age int } func (tmp Person) PrintInfo() {//定义Person的方法 fmt.Println("tmp = ", tmp) } func (p *Person) SetInfo (n string, s byte, a int) {//定义指针型的方法 p.name = n p.sex = s p.age = a } func main() { p := Person{"mike",'m', 18} //初始化 Person 值 p.PrintInfo() //调用方法 var p1 Person (&p1).SetInfo("bike", 'n', 17) //调用方法 p1.PrintInfo() }
运行结果如下:
robot@ubuntu:~/workspace-go/src/mothed$ go run mothed.go tmp = {mike 109 18} tmp = {bike 110 17}
package main import ( "fmt" ) type long int // 定义int 类型 long func (tmp long) Add(other long) long { //给 long 对象添加具有返回值的方法 return tmp+other; } func main() { var a long = 12 r := a.Add(3) fmt.Println("result = ",r) }
package main import ( "fmt" ) type Person struct { name string sex byte age int } func (p *Person) SetInfoPointer (n string, s byte, a int) { p.name = n p.sex = s p.age = a fmt.Printf("SetInfoPointer &p = %p\n",p) } func (p Person) SetInfoValue (n string, s byte, a int){ p.name =n p.sex = s p.age = a fmt.Printf("SetInfoValue &p = %p\n", &p) } func main() { var s1 Person = Person{"golan", 'm',24} fmt.Printf("&s1 = %p\n",&s1) s1.SetInfoValue("mike",'m',33) fmt.Println("s1 = ",s1) var s2 Person = Person{"golan",'m',24} fmt.Printf("s2 = %p\n",&s2) (&s2).SetInfoPointer("bike",'m',22) fmt.Println("s2 = ",s2) }
运行结果:
robot@ubuntu:~/workspace-go/src/mothed$ go run mothed.go &s1 = 0xc000026040 SetInfoValue &p = 0xc000026060 s1 = {golan 109 24} s2 = 0xc0000260a0 SetInfoPointer &p = 0xc0000260a0 s2 = {bike 109 22}
package main import ( "fmt" ) type Person struct { name string sex byte age int } type Worker struct { Person id int addr string } func (p *Person) PrintInfo () { fmt.Printf("name = %s, sex = %c,age = %d \n", p.name, p.sex, p.age) } func (w *Worker) PrintInfo() { fmt.Printf("name = %s, sex = %c, age = %d, addr = %s \n",w.name, w.sex, w.age, w.addr) } func main() { w := Worker{Person{"golan",'m',23},100,"haidian beijing"} w.PrintInfo() fmt.Printf("\n\r") w.Person.PrintInfo() fmt.Printf("\n\r") f := w.Person.PrintInfo f() f1 := (*Worker).PrintInfo f1(&w) }
输入内容如下
robot@ubuntu:~/workspace-go/src/mothed$ go run mothed.go name = golan, sex = m, age = 23, addr = haidian beijing name = golan, sex = m,age = 23 name = golan, sex = m,age = 23 name = golan, sex = m, age = 23, addr = haidian beijing
package main import ( "fmt" ) type Square struct { side float32 } type Rectangle struct { length, width float32 } type Shaper interface { Area() float32 } func (sq *Square) Area() float32 { return sq.side * sq.side } func (r *Rectangle) Area() float32 { return r.length * r.width } func main () { r := &Rectangle{10,2} q := &Square{10} fmt.Printf("r.area = %f\n",r.Area()) fmt.Printf("q.area = %f\n",q.Area()) shapes := []Shaper{r,q} for n, _ := range shapes { fmt.Println("图形数据: ", shapes[n]) fmt.Println("它的面积: ", shapes[n].Area()) } }
输出内容
robot@ubuntu:~/workspace-go/src/mothed$ go run mothed.go r.area = 20.000000 q.area = 100.000000 图形数据: &{10 2} 它的面积: 20 图形数据: &{10} 它的面积: 100
参考链接
https://zhuanlan.zhihu.com/p/94625212