Golang(4)Struct Type and Object Oriented

本文介绍了 Golang 中的结构体定义及其使用方法,包括结构体字段的初始化、结构体指针的使用,以及如何通过结构体实现面向对象编程。文章还详细解释了如何为结构体定义方法,并展示了如何利用接口实现多态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Golang(4)Struct Type and Object Oriented

2.4 Struct Type
type person struct {
     name string
     age int
}

var P person
P.name = “sillycat”
P.age = 32

P := person{“Kiko”, 18}
P := person{age:24, name:”Kiko"}

P is a pointer *person
P := new(person)

Compare the age function
return value will be the older person and the age difference
fund Older(p1, p2 person) (person, int) {
     if p1.age > p2.age {
          return p1, p1.age - p2.age
     }
     return p2, p2.age-p1.age
}

var tom person
tom.name, tom.age = “Tom”, 18

tb_Older, tb_diff := Older(tom, bob)

struct hidden property
Something like extend based Class

type Human struct {
     name string
     age int
     weight int
}

type Student struct{
     Human // all the properties will be in this struct then
     speciality string
}

mark:=Student{Human{“Mark”, 26,120}, “Computer”}
mark.age, mark.weight

mark.Human = Human{“Marcus”, 55, 220}
mark.Human.age = 1

Define other Type
type Skills []string

type Student struct {
     Human          //hidden property
     Skills             //hidden property
     int                  //hidden property
     speciality string
}

jane := Student{Human:Human{“Jane”, 35,100}, speciality:”biology"}
jane.Skills = []string{“anotomy”}
jane.Skills = append(jane.Skills, “physics”, “golang”)
jane.int = 3

2.5 Object Oriented
method

Old way is struct and func calculate the area
type Rectangle struct {
     width, height float64
}

fund area(r Rectangle) float64{
     return r.width*r.height
}

Method area() will belong to Rectangle, A method is a function with an implicit first argument, called a receiver.

func (r ReceiverType) funcName(parameters) (results)

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
}

Alias for the Type/ Customer Type
type ages int
type months map[string]int

Pointer as Receiver
when you want to change the value
func(b *Box)setColor(c Color){
     …snip...
}

method extension
type Human struct{
     name string
     ..snip...
}

type Student struct{
     Human
     school string
}

type Employee struct {
     Human
     company string
}

func (h Human) hi(){
     …snip...
}

mark :=Student…snip…
sam := Emloyee…snip…
mark.hi()
sam.hi()

Override the method
func (e Employee) hi(){
     ..snip...
}

2.6 Interface
…snip...


References:
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/02.4.md

Golang1 ~ 3
http://sillycat.iteye.com/blog/2037798
http://sillycat.iteye.com/blog/2047158
http://sillycat.iteye.com/blog/2052936

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值