GO语言练习:组合的用法

Go语言面向对象编程实践
本文详细介绍了使用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

 

转载于:https://www.cnblogs.com/fengbohello/p/4625467.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值