当前位置: 首页 > wzjs >正文

学士学位网站重置密码怎么做营销比较成功的企业

学士学位网站重置密码怎么做,营销比较成功的企业,html制作静态网站模板,wordpress+下载受限GO语言学习(二) method(方法) 这一节我们介绍一下GO语言的面向对象,之前我们学习了struct结构体,现在我们来解释一下方法method主要是为了简化代码,在计算同类时,使用函数接收方法…

GO语言学习(二)

method(方法)

这一节我们介绍一下GO语言的面向对象,之前我们学习了struct结构体,现在我们来解释一下方法method主要是为了简化代码,在计算同类时,使用函数接收方法可以极大的简化代码量。简单来说就是使用receiver来作为method的主体。

下面给一个具体的事例:

package mainimport ("fmt""math"
)type Rectangle struct {width, height float64
}type Circle struct {radius float64
}func (r Rectangle) area() float64 {return r.width*r.height
}func (c Circle) area() float64 {return c.radius * c.radius * math.Pi
}// Rectangle存在字段 height 和 width, 同时存在方法area(), 这些字段和方法都属于Rectangle。
func main() {r1 := Rectangle{12, 2}r2 := Rectangle{9, 4}c1 := Circle{10}c2 := Circle{25}fmt.Println("Area of r1 is: ", r1.area())fmt.Println("Area of r2 is: ", r2.area())fmt.Println("Area of c1 is: ", c1.area())fmt.Println("Area of c2 is: ", c2.area())
}

在method方法中有一些要注意的点,我在这里为大家列出一下:

1.虽然method的名字一模一样,但是如果接收者不一样,那么method就不一样(接受者意为接受主体)
2.method里面可以访问接收者的字段
3.调用method通过`.`访问,就像struct里面访问字段一样

自定义类型

自定义类型是一个基于人为自己设定的类型,struct相当于特定的自定义类型,基本结构为type typeName typeLiteral,从中可以看出实际上只是一个定义了一个别名,有点类似于c中的typedef,下面给一个具体的事例。

type ages inttype money float32type months map[string]intm := months {"January":31,"February":28,..."December":31,
}

下面我们来给给具体的例子,方便后面的讲解。

package mainimport "fmt"const(WHITE = iotaBLACKBLUEREDYELLOW
)type Color bytetype Box struct {width, height, depth float64color Color
}type BoxList []Box //a slice of 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.00k := Color(WHITE)for _, b := range bl {if bv := b.Volume(); bv > v {v = bvk = b.color}}return k
}func (bl BoxList) PaintItBlack() {for i := range bl {bl[i].SetColor(BLACK)}
}func (c Color) String() string {strings := []string {"WHITE", "BLACK", "BLUE", "RED", "YELLOW"}return strings[c]
}func main() {boxes := BoxList {Box{4, 4, 4, RED},Box{10, 10, 1, YELLOW},Box{1, 1, 20, BLACK},Box{10, 10, 1, BLUE},Box{10, 30, 1, WHITE},Box{20, 20, 20, YELLOW},}fmt.Printf("We have %d boxes in our set\n", len(boxes))fmt.Println("The volume of the first one is", boxes[0].Volume(), "cm³")fmt.Println("The color of the last one is",boxes[len(boxes)-1].color.String())fmt.Println("The biggest one is", boxes.BiggestColor().String())fmt.Println("Let's paint them all black")boxes.PaintItBlack()fmt.Println("The color of the second one is", boxes[1].color.String())fmt.Println("Obviously, now, the biggest one is", boxes.BiggestColor().String())
}

我们来解释一下这段代码,从自定义类型和接收者定义方法来解释:

  • Color作为byte的别名

  • 定义了一个struct:Box,含有三个长宽高字段和一个颜色属性

  • 定义了一个slice:BoxList,含有Box

  • Volume()定义了接收者为Box,返回Box的容量

  • SetColor(c Color),把Box的颜色改为c

  • BiggestColor()定在在BoxList上面,返回list里面容量最大的颜色

  • PaintItBlack()把BoxList里面所有Box的颜色全部变成黑色

  • String()定义在Color上面,返回Color的具体颜色(字符串格式)

指针为接收者

我们前面传的接受者其实是一个copy的副本,改变这个副本的值是不会影响真实的值,这点偏向于实际的构造问题,故我们不能在这改变真实的值,因此引入了指针来改变实际的值。

这里你也许会问了那SetColor函数里面应该这样定义*b.Color=c,而不是b.Color=c,因为我们需要读取到指针相应的值,其实Go里面这两种方式都是正确的,当你用指针去访问相应的字段时(虽然指针没有任何的字段),Go知道你要通过指针去获取这个值,看到了吧,Go的设计是不是越来越吸引你了。

所以在实际开发中你不用担心你是调用的指针的method还是不是指针的method。

方法继承

首先可以使用匿名字段来实现继承,在这里面中匿名字段实现了一个method,那么包含这个匿名字段的struct也能调用该method。

让我们来看下面这个例子:

package mainimport "fmt"type Human struct {name stringage intphone string
}type Student struct {Human //匿名字段school string
}type Employee struct {Human //匿名字段company string
}//在human上面定义了一个method
func (h *Human) SayHi() {fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone)
}func main() {mark := Student{Human{"Mark", 25, "222-222-YYYY"}, "MIT"}sam := Employee{Human{"Sam", 45, "111-888-XXXX"}, "Golang Inc"}mark.SayHi() // 相当于继承的使用了接收者为*human的方法sam.SayHi()
}

方法重写

在使用方法中通过Employee实现SayHi,可以参考匿名字段冲突一样的道理,我们可以在Employee上面定义一个method,重写了匿名字段的方法。

参考代码如下:

package mainimport "fmt"type Human struct {name stringage intphone string
}type Student struct {Human //匿名字段school string
}type Employee struct {Human //匿名字段company string
}//Human定义method
func (h *Human) SayHi() {fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone)
}//Employee的method重写Human的method
func (e *Employee) SayHi() {fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name,e.company, e.phone) //Yes you can split into 2 lines here.
}func main() {mark := Student{Human{"Mark", 25, "222-222-YYYY"}, "MIT"}sam := Employee{Human{"Sam", 45, "111-888-XXXX"}, "Golang Inc"}mark.SayHi()sam.SayHi()
}

这个重写方法很像python的重写,相当于写出不同的方法来实现方法的重写。

总结

在GO语言的面向对象编程中没有啥关键字和标识符来标记范围,因此GO语言使用大小写来识别是公有还是私有,方法是非常重要的概念需要大家着重掌握,不会的可以在评论区私我,这一节就先讲到这里,在这里助友友们周末快乐。


文章转载自:

http://JOdes716.xmnLc.cn
http://Q1TX7fSn.xmnLc.cn
http://SZ8oaAvD.xmnLc.cn
http://8KQTQWHo.xmnLc.cn
http://TxeXSRVQ.xmnLc.cn
http://pw4m9Vx6.xmnLc.cn
http://VDLO4y5r.xmnLc.cn
http://O9MJDgW7.xmnLc.cn
http://JbGCmXI0.xmnLc.cn
http://ukW7r85z.xmnLc.cn
http://rB0djgf0.xmnLc.cn
http://hdO8XVeL.xmnLc.cn
http://FtSH5gCO.xmnLc.cn
http://YAzOrJrV.xmnLc.cn
http://NXt9ErRA.xmnLc.cn
http://JBAJ63N6.xmnLc.cn
http://zBJTVHPq.xmnLc.cn
http://KgJJGhHT.xmnLc.cn
http://66BO9LEC.xmnLc.cn
http://2MFDk41S.xmnLc.cn
http://YFvSz1Z8.xmnLc.cn
http://FwP45WHB.xmnLc.cn
http://ustAuZWA.xmnLc.cn
http://mfcX3pwB.xmnLc.cn
http://OuBZQM3q.xmnLc.cn
http://hbghDftF.xmnLc.cn
http://KrOwCwmZ.xmnLc.cn
http://OLiFtY1s.xmnLc.cn
http://tjnDZs7v.xmnLc.cn
http://aswBrZxv.xmnLc.cn
http://www.dtcms.com/wzjs/645064.html

相关文章:

  • 网站信息组织优化公司网站搭建
  • 网站搭建免费软件建筑用模板多少钱一张
  • 网站做等保备案微信分销工具
  • 推广网站推广都江堰做网站
  • 最适合新人的写作网站网站开发项目组团队
  • 四川电大住房和城乡建设厅网站网站介绍视频怎么做的
  • 做网站类的书本信息wordpress 导航网站主题
  • 网站模版怎样使用wordpress精华主题
  • wordpress 建站 域名网站首页权重低
  • 织梦可以做论坛网站网站设计需要多少钱
  • 做彩妆发哪个网站浏览量高网页设计的网站
  • 整合营销传播理论厦门网站做优化
  • 品牌网站建设4小蝌蚪山东省交通厅建设网站首页
  • 开封开封县网站建设北碚区建设银行网站
  • 网站建设方案 流程泰安市两学一做网站
  • 网站建设服务市场分析服务好的常州网站建设
  • 毕业设计网页制作咖啡网站图片wordpress为静态
  • 公司网站免费自建网站建设与运营课程
  • 备案 网站首页网址企业网站教程 优帮云
  • 电子商务网站功能介绍北京做网站公司排名浩森宇特
  • 做高铁在哪个网站买企业邮箱怎么用
  • 西宁网站建设 哪家好张家界网站建设的公司
  • 纯静态企业网站模板免费下载建设行政主管部门官方网站
  • 长沙专业建网站公司网站增加聊天
  • 珠宝营销型网站wordpress内网和外网
  • win7架设asp网站自己做的砍价网站
  • 郴州网站seo外包廊坊电子商务网站建设
  • 网站设计目标 优帮云微信知彼网络网站建设
  • 做网站商城如何优化查答案的网站制作模板
  • 企业网站设计能否以黑科技网站