Object-oriented

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()}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,490评论 0 10
  • 还记得当年MD风格刚出现的时候,还真是掀起了一股热潮;个人平时比较喜欢研究一些View相关的东西,这里呢就一时兴起...
    Jony_Huang阅读 547评论 0 4
  • 别回头 最好的在前面等你
    苏子末阅读 139评论 0 0
  • 高考临近,作为绝密的高考卷已经成型,除了个别参与命题的人知道试卷的内容,任何人都无法窥探到试卷的神秘面容,...
    玲荷阅读 202评论 0 0
  • 好友寄了一封信给我,在未到前跟我说她用了Baymax的信封。然后被我说她少女心复发。她说,让我说的有点好像她有心脏...
    惑者阅读 315评论 1 0