Go语言(二) 继承和重载

继承

 1 package main
 2 
 3 import "fmt"
 4 
 5 type Skills  []string
 6 
 7 type person struct {
 8   name string
 9   age  int
10   weight int
11 }
12 
13 type Student struct {
14   person    //继承
15   Skills
16   int
17   spe string
18 }
19 
20 func init() {
21 
22 }
23 
24 func main() {
25   xuxu := Student{person{"xuxu",25,68}, []string{"anatomy"}, 1, "boy"} //方式一,全部指定
26   jane := Student{person:person{"Jane",25,100}, spe:"Biology"}  //方式二,指哪打哪
27 
28   fmt.Printf("His name is %s\n", jane.name)
29   fmt.Printf("His name is %s\n", xuxu.name)
30 }

 

 

重载

 1 package main
 2 
 3 import "fmt"
 4 
 5 type Skills  []string
 6 
 7 type person struct {
 8   name string
 9   age  int
10   weight int
11   spe  string  //inner spe,重载
12 }
13 
14 type Student struct {
15   person    //继承
16   Skills
17   int
18   spe string   //outter spe,重载
19 }
20 
21 func init() {
22 
23 }
24 
25 func main() {
26   xuxu := Student{person{"xuxu",25,68,"inner spe"}, []string{"anatomy"}, 1, "outter spe"} 
27 
28   fmt.Printf("His name is %s\n, inner spe :%, outter spe :%s", xuxu.name, xuxu.person.spe, xuxu.spe)
29 }

 

 

转载于:https://www.cnblogs.com/xuxu8511/p/3296546.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值