golang学习之--struct类型
来源:互联网 发布:韩春雨 知乎精华 编辑:程序博客网 时间:2024/05/31 19:51
GO语言中的struct和c或其他语言一样,我们可以声明新的类型
eg:
type Human struct {
name string
age int
}
package mainimport (. "fmt". "strconv")type Human struct {name stringage intsex stringphone string}type student struct {Human // 匿名字段sno stringphone string}type teacher struct {Human //匿名字段tno stringphone string}type print interface { //定义一个接口,但凡实现了String()方法的类型也就实现了这个接口String() string}type personalPhone interface {print //嵌入的interfacesetPhone(phone string)}func (h Human) String() string { //Human类型的String()方法return "The hunman's name is " + h.name + " age is " + Itoa(h.age) + " sex is " + h.sex + " phone is " + h.phone}func (s student) String() string { //Student类型的String()方法return "The student's name is " + s.name + " age is " + Itoa(s.age) + " sex is " + s.sex + " personal phone is " + s.phone + " phone is " + s.Human.phone}func (t teacher) String() string { //Teacher类型的String()方法return "The teacher's name is " + t.name + " age is " + Itoa(t.age) + " sex is " + t.sex + " personal phone is " + t.phone + " phone is " + t.Human.phone}func (s *student) setPhone(phone string) {s.Human.phone = phone}func (s *student) setSno(sno string) {s.sno = sno}func (t *teacher) setPhone(phone string) {t.Human.phone = phone}func (t *teacher) setTno(tno string) {t.tno = tno}func main() {h := Human{"yuhanhu", 22, "men", "13307149145"} //初始化赋值s := student{Human{"fanzhang", 22, "men", "13012526545"}, "1110322133", "13307149168"}t := teacher{Human{"viease", 28, "men", "13104715426"}, "110101", "12312312313"}p := make([]print, 3) //slice,interface, channel需要用makep[0], p[1], p[2] = h, s, tfor _, i := range p {Println(i)}pp := make([]personalPhone, 2)pp[0], pp[1] = &s, &tpp[0].setPhone("110")pp[1].setPhone("119")Println(pp[0])Println(pp[1])}
0 0
- golang学习之--struct类型
- Golang 学习之路四:类型
- golang学习之Interface类型断言
- golang学习笔记之引用类型与值类型
- golang结构体struct学习笔记
- golang struct
- Golang学习笔记:语言规范之词汇元素和类型
- Golang学习笔记:语言规范之类型(续)
- Golang 学习之路七:数据(Array、Slice、Map、Struct)
- C#学习日记09---数据类型 之 结构(Struct)类型
- golang 类型断言的学习
- GO系列之struct类型
- c++学习之struct
- Golang 奇葩的时间包之Json与struct对象之间互相转换,JsonDate和JsonTime自定义类型如何实现json编码和解码(二)
- C++学习笔记之五 复合类型之结构体(struct)
- golang struct 面向对象
- golang struct to map
- golang struct继承测试
- 空间统计(三)聚类分布制图
- Kafka文件存储机制那些事
- 客户端跨域访问(CROS)之jsonp详解
- c++技巧- 用vector和unique删去重复元素
- Service启动及参数
- golang学习之--struct类型
- InstallShield 自定义窗口控件控制
- hdu 1232 并查集
- No message body writer has been found for class
- Android task和back stack详解
- 经典智力问题收集
- android开发之pinyin4j的基本用法
- 黑马程序员——Java学习之模拟银行业务调度系统
- OpenGL的SWT的控件