GO语言练习:组合的用法

1、代码

2、运行


 

1、代码

 1 package main
 2 
 3 import "fmt"
 4 
 5 type Base struct {
 6     Name string
 7 }
 8 
 9 func (base * Base) Foo() {
10     fmt.Println("Base Foo : ", base.Name)
11 }
12 
13 func (base * Base) Bar() {
14     fmt.Println("Base Bar : ", base.Name)
15 }
16 
17 type Foo struct {
18     Base
19     a int 
20 }
21 
22 func (foo * Foo) Bar() {
23     foo.Base.Bar()
24     fmt.Println("\tFoo Bar : ", foo.Name)
25 }
26 
27 func main() {
28     var str string = "hello world"
29 
30     base := &Base{str}
31     base.Foo()
32 
33     str = "Ni hao"
34     foo := &Foo{Base{str}, 0}
35     foo.Bar()
36     foo.Foo()
37 }

2、运行

$ go run combination.go 
Base Foo :  hello world
Base Bar :  Ni hao
    Foo Bar :  Ni hao
Base Foo :  Ni hao

 

posted @ 2015-07-06 21:27  fengbohello  阅读(675)  评论(0编辑  收藏  举报