铜仁建设集团招聘信息网站,清空wordpress,网页微信登录不了,怎么在拼多多开无货源网店文章目录 方法:one: 方法的概念:star2: 内嵌类型的方法和继承:star2: 多重继承 #x1f4c5; 2024年5月9日 #x1f4e6; 使用版本为1.21.5
方法
1️⃣ 方法的概念
⭐️ 在Go语言中没有类这个概念#xff0c;可以使用结构体来实现#xff0c;那类方法呢#xff1f;Go也… 文章目录 方法:one: 方法的概念:star2: 内嵌类型的方法和继承:star2: 多重继承 2024年5月9日 使用版本为1.21.5
方法
1️⃣ 方法的概念
⭐️ 在Go语言中没有类这个概念可以使用结构体来实现那类方法呢Go也同样的实现那就是方法
⭐️ 方法和函数长得很像但是它多出来了一个接收者的选项我也不知道为啥要这么高大上的名字看书的时候都给我看蒙蔽了其实就是和一个结构体(类)或者一个类型进行绑定比如下面的列子
this表示one这个类和java中的this或者python的self类似表示当前类也就是表示这个方法是属于one类的
type one struct {a intb int
}func (this one) GetA() {//this类似于java中的this表示这个类println(this.a)
}func main() {one : one{1, 2} //这里有没有很像创建一个类one.GetA() //然后调用类方法
}
⭐️ 接收者的类型可以是int,bool,string,数组函数的别名(经过type定义的)类型但是不能是一个接口类型接口是一个抽象定义方法是具体的实现而且还不能是一个指针类型但是可以是可以是前面几种类型的指针类型
⭐️ 在go中方法和类可以不用写在一个源代码(体现的很明显了)可以分开写但是必须在同一个包内
⭐️ 方法在一个接收者类型中只能存在一个(名字)不能存在多个但是可以在不同接收者类型中多个(名字)
//不同的接收者类型
func (this two) GetA() { println(this.a)
}func (this one) GetA() { println(this.a)
}⭐️ 如果想定义的方法可以在外部调用(你可以发现Go中已经实现的方法如fmt.Println它们都是开头首字母大写)前面说过需要首字母大写
⭐️ 绑定类型(传入接收者)最好是使用指针类型否则传入的接收者类只是类的一个拷贝下面这个列子就体现出来了 ps: 这里的Set和Get很像java对不对 type one struct {a intb int
}func (this one) GetA() {println(this.a)
}
func (this one) SetA(i int) int { //修改athis.a ireturn this.a
}func main() {one : one{1, 2} one.GetA() //输出1one.SetA(3) //使用Set方法修改为 3one.GetA() //输出1
}如果换成指针
type one struct {a intb int
}func (this *one) GetA() {println(this.a)
}
func (this *one) SetA(i int) int { //使用指针类型this.a ireturn this.a
}func main() {one : one{1, 2}one.GetA() //输出1one.SetA(3) //使用Set方法修改为 3one.GetA() //输出3
} 学到这里我直接懂了这玩意和类差不多甚至可以可以定义自己的get、set方法包括toString也可以(幸好之前学完了java)
type Person struct {Name stringage intsex string
}func (this *Person) GetName() { //get方法println(this.Name)
}
func (this *Person) SetName(i string) string { //set方法this.Name ireturn this.Name
}
func (this *Person) ToString() { //Tostring方法fmt.Println(name :, this.Name,, age :, this.age,, sex :, this.sex,)
}func main() {p1 : Person{zhangsan, 18, man}p1.ToString()
}⭐️ 对象的字段不应该由2个或2个以上的不同线程在同一时间去改变(在后面会学解决办法) 内嵌类型的方法和继承
⭐️ 一个匿名类型被嵌入到结构体中匿名类型的可见方法也同样被内嵌相当于继承 多重继承
⭐️ 简单来说就是一个一个子类可以继承多个父类在Java中是不被允许的但是在Go结构体匿名字段下可以实现只需要通过内嵌两个就好了
type Person struct { //人的基本Name stringage intsex string
}type Student struct { //学生School stringscore intranked int
}
type You struct { //你Person //你是个人也是一个学生Student
}func main() {p1 : You{Person: Person{lisi, 18, 男},Student: Student{茶啊二中, 99, 1},}fmt.Println(p1)
}
文章转载自: http://www.morning.gfprf.cn.gov.cn.gfprf.cn http://www.morning.nhgfz.cn.gov.cn.nhgfz.cn http://www.morning.czgfn.cn.gov.cn.czgfn.cn http://www.morning.ytnn.cn.gov.cn.ytnn.cn http://www.morning.rwxnn.cn.gov.cn.rwxnn.cn http://www.morning.vaqmq.cn.gov.cn.vaqmq.cn http://www.morning.rxlk.cn.gov.cn.rxlk.cn http://www.morning.ndxrm.cn.gov.cn.ndxrm.cn http://www.morning.mlcwl.cn.gov.cn.mlcwl.cn http://www.morning.jjzbx.cn.gov.cn.jjzbx.cn http://www.morning.jzlfq.cn.gov.cn.jzlfq.cn http://www.morning.tktyh.cn.gov.cn.tktyh.cn http://www.morning.tktcr.cn.gov.cn.tktcr.cn http://www.morning.ppbqz.cn.gov.cn.ppbqz.cn http://www.morning.beeice.com.gov.cn.beeice.com http://www.morning.mzrqj.cn.gov.cn.mzrqj.cn http://www.morning.rlqml.cn.gov.cn.rlqml.cn http://www.morning.dnycx.cn.gov.cn.dnycx.cn http://www.morning.hrpjx.cn.gov.cn.hrpjx.cn http://www.morning.rnnwd.cn.gov.cn.rnnwd.cn http://www.morning.ldqzz.cn.gov.cn.ldqzz.cn http://www.morning.fhqdb.cn.gov.cn.fhqdb.cn http://www.morning.yixingshengya.com.gov.cn.yixingshengya.com http://www.morning.nqyfm.cn.gov.cn.nqyfm.cn http://www.morning.wfysn.cn.gov.cn.wfysn.cn http://www.morning.hjjkz.cn.gov.cn.hjjkz.cn http://www.morning.pphgl.cn.gov.cn.pphgl.cn http://www.morning.wnjsp.cn.gov.cn.wnjsp.cn http://www.morning.kntbk.cn.gov.cn.kntbk.cn http://www.morning.gypcr.cn.gov.cn.gypcr.cn http://www.morning.nzfyx.cn.gov.cn.nzfyx.cn http://www.morning.fdhwh.cn.gov.cn.fdhwh.cn http://www.morning.txrq.cn.gov.cn.txrq.cn http://www.morning.svrud.cn.gov.cn.svrud.cn http://www.morning.ynstj.cn.gov.cn.ynstj.cn http://www.morning.aa1585.com.gov.cn.aa1585.com http://www.morning.rmfw.cn.gov.cn.rmfw.cn http://www.morning.nclbk.cn.gov.cn.nclbk.cn http://www.morning.wjqyt.cn.gov.cn.wjqyt.cn http://www.morning.mgtrc.cn.gov.cn.mgtrc.cn http://www.morning.jspnx.cn.gov.cn.jspnx.cn http://www.morning.qhydkj.com.gov.cn.qhydkj.com http://www.morning.kzslk.cn.gov.cn.kzslk.cn http://www.morning.rwwdp.cn.gov.cn.rwwdp.cn http://www.morning.nqypf.cn.gov.cn.nqypf.cn http://www.morning.xdpjs.cn.gov.cn.xdpjs.cn http://www.morning.haolipu.com.gov.cn.haolipu.com http://www.morning.nfmlt.cn.gov.cn.nfmlt.cn http://www.morning.fyskq.cn.gov.cn.fyskq.cn http://www.morning.mqzcn.cn.gov.cn.mqzcn.cn http://www.morning.frpfk.cn.gov.cn.frpfk.cn http://www.morning.lqytk.cn.gov.cn.lqytk.cn http://www.morning.rybr.cn.gov.cn.rybr.cn http://www.morning.lgpzq.cn.gov.cn.lgpzq.cn http://www.morning.nshhf.cn.gov.cn.nshhf.cn http://www.morning.nfccq.cn.gov.cn.nfccq.cn http://www.morning.zlwg.cn.gov.cn.zlwg.cn http://www.morning.rhjsx.cn.gov.cn.rhjsx.cn http://www.morning.qynpw.cn.gov.cn.qynpw.cn http://www.morning.lhxrn.cn.gov.cn.lhxrn.cn http://www.morning.lhztj.cn.gov.cn.lhztj.cn http://www.morning.qcfgd.cn.gov.cn.qcfgd.cn http://www.morning.zrlms.cn.gov.cn.zrlms.cn http://www.morning.nfpgc.cn.gov.cn.nfpgc.cn http://www.morning.fcwb.cn.gov.cn.fcwb.cn http://www.morning.sh-wj.com.cn.gov.cn.sh-wj.com.cn http://www.morning.yrjkz.cn.gov.cn.yrjkz.cn http://www.morning.btypn.cn.gov.cn.btypn.cn http://www.morning.tlnbg.cn.gov.cn.tlnbg.cn http://www.morning.rrcxs.cn.gov.cn.rrcxs.cn http://www.morning.pxsn.cn.gov.cn.pxsn.cn http://www.morning.smhtg.cn.gov.cn.smhtg.cn http://www.morning.mmjyk.cn.gov.cn.mmjyk.cn http://www.morning.cwqpl.cn.gov.cn.cwqpl.cn http://www.morning.ptwrz.cn.gov.cn.ptwrz.cn http://www.morning.bbgr.cn.gov.cn.bbgr.cn http://www.morning.kjyfq.cn.gov.cn.kjyfq.cn http://www.morning.yhtnr.cn.gov.cn.yhtnr.cn http://www.morning.mnpdy.cn.gov.cn.mnpdy.cn http://www.morning.kksjr.cn.gov.cn.kksjr.cn