当前位置: 移动技术网 > IT编程>脚本编程>Go语言 > [Go] Golang中的面向对象

[Go] Golang中的面向对象

2019年11月28日  | 移动技术网IT编程  | 我要评论

struct interface 就可以实现面向对象中的继承,封装,多态

继承的演示:
tsh类型继承people类型,并且使用people类型的方法

多态的演示
tsh类型实现了接口student,实现了接口定义的方法

完整代码:

package main

import "fmt"

//父类型
type people struct {
}

func (p *people) echo() {
    fmt.println("taoshihan")
}

//接口
type student interface {
    do()
}

//子类型,实现了接口,继承了父类型
type tsh struct {
    people
}

func (t tsh) do() {
    fmt.println("taoshihan do")
}
func main() {
    //继承的演示
    t := tsh{people{}}
    t.echo()
    //多态的演示
    var student student
    student = t
    student.do()
}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网