码迷,mamicode.com
首页 > 其他好文 > 详细

golang的面向对象实现

时间:2018-10-27 13:19:59      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:Golan   wim   fish   func   继承   通过   接口   动物   play   

GoLang的面向对象

通过golang实现面向对象的过程, 以及golang的继承关系

定义了动物, 定义了鸟类,鱼类, 最后的对象是 燕子, Nimo

package main

import "fmt"

type Animal interface {
    Eat()
    Play()
}
type animal struct {
    name string
}

type Bird interface {
    Animal //匿名嵌入接口
    Fly()
}

type bird struct {
    animal //匿名嵌入继承父亲的name
    wings  int
}

type Fish interface {
    Animal
    Swim()
}

type fish struct {
    animal //集成父亲的name
    name   string
    fin    int
}

func (a *animal) Eat() {
    fmt.Println(a.name, "is eatting now")
}

func (a *animal) Play() {
    fmt.Println(a.name, "is playing now")
}

func (b *bird) Fly() {
    fmt.Println(b.name, "is flying now")
}

func (b *bird) Eat() { // 重写了父亲的吃的方法
    fmt.Println(b.name, "is eatting too much")
}

func (f *fish) Swim() {
    fmt.Println(f.name, "is swimming now")
}

func (f *fish) Eat() {
    fmt.Println(f.name, "is etting now")
}

func main() {
    fmt.Println("hello golang oo")

    bird := bird{animal{"swallow"}, 2}

    bird.Fly() // bird的特有方法,    "swallow is flying now"
    bird.Eat() //重写了父亲的方法,  "swallow is eatting too much"

    fish := fish{animal{"Clown Fish"}, "Nimo", 3}

    fish.Play() // 继承父亲的方法, "Clown Fish is playing now", 父亲的name
    fish.Eat()  // 重写父亲的方法, "Nimo is etting", 用父亲的name
    fish.Swim() // fish 特有的方法 "Nimo is swimming now";
}

golang的面向对象实现

标签:Golan   wim   fish   func   继承   通过   接口   动物   play   

原文地址:https://www.cnblogs.com/gpan/p/9860586.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!