zoukankan      html  css  js  c++  java
  • [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() {
        fmt.Println("taoshihan do")
    }
    func main() {
        //继承的演示
        t := Tsh{People{}}
        t.echo()
        //多态的演示
        var student Student
        student = t
        student.Do()
    }
  • 相关阅读:
    Js变量类型
    前端面试题HTML
    AP聚类
    锚点
    html
    Active Learning主动学习
    z-index
    position
    学习笔记_卷积神经网络
    学习笔记_深度学习的数学基础
  • 原文地址:https://www.cnblogs.com/taoshihan/p/11945876.html
Copyright © 2011-2022 走看看