Object-oriented

曹小恒 · · 607 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

Method belog to struct

package mainimport (    "fmt"    "math")type Rectangle struct{    width, height float64}type Circle struct {    radius float64}func (r Rectangle) area(){    return r.width*r.height}func (c Circle) area(){    return c.radius * c.radius * math.Pi}func main(){    r1 := Rectangle{12,2}    c1 := Circle{10}    fmt.Println("Area of r1 is: ",r1.area())}

Noted:

  • the name of methods can be the same, but if the struct receivers are different, then the methods are different

  • calling method by ., just like struct calling its elements.

[图片上传失败...(image-227a09-1540312119818)]

Not only struct, all other build-in types, self-defined types could be the object to apply methods.

Here are some examples of self-defined types:

type ages int //aliastype money float32type months map[string]int

Here is another comprehensive example:

package mainimport "fmt"const(    white = iota    black    blue    red    yellow)type Color byte    //uint8type Box struct{    width,height, depth float64    color Color}type BoxList []Box  //a slice whose elements are boxesfunc (b Box) Volume() float64{    return b.width * b.height * b.depth}func (b *Box) SetColor(c color){    b.color = c}func (bl BoxList) BiggestColor() Color{    v := 0.00    k := Color(white)    for _,b := range bl{    //_,b corresponding to index and value, b is the strcuts        if bv := b.Volume();bv>v{            v = bv            k = b.color        }    }    return k}func (bl BoxList) PaintItBlack(){    for i := range bl{    //here i is index, we skip the value, we can also write like this:    //fot _,b := range bl        bl[i].SetColor(black)    //b.SeetColor(black)    }}func (c Color) String() string{    strings := []string {"white","black","blue","red","yellow"}    return string[c]}

Use pointers as receiver

func (b *Box) SetColor(c color){    b.color = c}

Here we may use *b.color instead, cause the input is a pointer(the address), however golang knows what we want to do, so we do not need to take too much time specifying those troublesome issues.

Likely, we may also use &bl[i] since the receiver of SetColor is a pointer(address), but similarily, do not focus on those details.

func (bl BoxList) PaintItBlack(){    for i := range bl{    //here i is index, we skip the value, we can also write like this:    //fot _,b := range bl        bl[i].SetColor(black)    //b.SeetColor(black)    }}

Inherit

If the embeded segment realize some methods, then the struct who includes it can inherit those methods, too.

package mainimport "fmt"type Human struct{    name string    age int    phone string}type Student struct{    Human    school string}func (h *Human) Sayhi(){    fmt.Println("Hi, I am", h.name, "you can call me on ",h.phone)}func main(){    make := Student{Human{"Mark",25,"222-222"},"MIT"}    make.SayHi()}

Overwrite

package mainimport "fmt"type Human struct{    name string    age int    phone string}type Student struct{    Human    school string}func (h *Human) Sayhi(){    fmt.Println("Hi, I am", h.name, "you can call me on ",h.phone)}func (s *Student) Sayhi(){    fmt.Println("Hi, I am a student")}func main(){    make := Student{Human{"Mark",25,"222-222"},"MIT"}    make.SayHi()}

有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:曹小恒

查看原文:Object-oriented

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

607 次点击  
加入收藏 微博
上一篇:Concurrence
下一篇:Introduction
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传