[Go] Golang中的面向对象

前端之家收集整理的这篇文章主要介绍了[Go] Golang中的面向对象前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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() {
    taoshihan do)
}
func main() {
    继承的演示
    t := Tsh{People{}}
    t.()
    多态的演示
    var student Student
    student = t
    student.Do()
}

 

猜你在找的Go相关文章