首页 > 其他 > 详细

[Go] Golang中的面向对象

时间:2019-11-27 23:42:11      阅读:69      评论:0      收藏:0      [点我收藏+]

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()
}

 

[Go] Golang中的面向对象

原文:https://www.cnblogs.com/taoshihan/p/11945876.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!