go 面向对象

一 struct类型

二 interface类型

面向对象3个特性:封装 继承 多态

1.封装(将方法或成员变量赋予一个结构体)

type cxx struct {

        body string

}

func (c *cxx) work(){

        fmt.Println(c.body)

}

func main(){

        c := cxx{body: 'strong'}

        c.work()

}

2.继承(子结构体拥有父级的方法)

type father struct {

        body string

}

type son struct {

        father

}

func (f *father) keep(){

        fmt.Println(f.body)

}

func main(){

        s := son{ father{ body:'strong' } } 

        s.keep()

}

3. interface (实现了这个接口的方法的结构体,该结构体的实例就和接口一个类型)(隐式声明)

type animal interface{

        eat()

}

type cat struct {}

type dog struct {}

func (c cat) eat(){}

func (d dog) eat(){}

func main(){

        var a animal

        a = cat{}

        a = dog{}

        // cat 结构体 和 dog 结构体 和 animal 属于一个类型

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值