美文网首页
【Go 精选】基本语法 - 继承和接口

【Go 精选】基本语法 - 继承和接口

作者: 熊本极客 | 来源:发表于2022-12-24 12:14 被阅读0次

1.如何利用结构体实现继承

package inheritance

import "fmt"

type Pet struct {
    name string
    age  int
}

func (p *Pet) ShowA() {
    fmt.Printf("name is %s, age is %d\n", p.name, p.age)
}

func (p *Pet) ShowB() {
    fmt.Println("能文能武的宠物")
}

type Dog struct {
    Pet
}

func (dog *Dog) ShowB() {
    fmt.Println("自带导航汪汪汪")
}

Dog 继承 Pet,重写函数 ShowB()

package inheritance

import "testing"

func TestAnimal(t *testing.T) {
    pet := &Pet{"name1", 1}
    pet.ShowA()
    pet.ShowB()

    dog := new(Dog)
    dog.name = "name2"
    dog.age = 2
    dog.ShowA()
    dog.ShowB()
}

2.如何使用接口

① 不需要显式声明实现了某个接口
② 多个类型可以实现同一个接口
③ 一个类型可以实现多个接口

package interfaces

type Shaper interface {
    SetSide(side float32)
    Area() float32
}

type Square struct {
    side float32
}

func (sq *Square) SetSide(side float32) {
    sq.side = side
}

func (sq *Square) Area() float32 {
    return sq.side * sq.side
}

Square 类型实现接口 Shaper

package interfaces

func TestInterfaces(t *testing.T) {
    var shaper Shaper
    shaper = new(Square)
    shaper.SetSide(2)
    fmt.Printf("The square has area: %f\n", shaper.Area())
}

3.实战继承和接口 —— 工厂模式

package factorymethod

// Operator 是被封装的实际类接口
type Operator interface {
    SetA(int)
    SetB(int)
    Result() int
}

// OperatorBase 是Operator 接口实现的基类,封装公用方法
type OperatorBase struct {
    a, b int
}

// SetA 设置 A
func (o *OperatorBase) SetA(a int) {
    o.a = a
}

// SetB 设置 B
func (o *OperatorBase) SetB(b int) {
    o.b = b
}

// PlusOperator Operator 的实际加法实现
// 结构体嵌套实现“继承”
type PlusOperator struct {
    OperatorBase
}

func (o *PlusOperator) Result() int {
    return o.a + o.b
}

// OperatorFactory 是工厂接口
type OperatorFactory interface {
    Create() Operator
}

// PlusOperatorFactory 是 PlusOperator 的工厂类
type PlusOperatorFactory struct{}

func (PlusOperatorFactory) Create() Operator {
    return &PlusOperator{OperatorBase{}}
}

// MinusOperator Operator 的实际减法实现
type MinusOperator struct {
    OperatorBase
}

func (o *MinusOperator) Result() int {
    return o.a - o.b
}

// MinusOperatorFactory 是 PlusOperator 的工厂类
type MinusOperatorFactory struct{}

func (MinusOperatorFactory) Create() Operator {
    return &MinusOperator{OperatorBase{}}
}
package factorymethod

import "testing"

func compute(factory OperatorFactory, a, b int) int {
    op := factory.Create()
    op.SetA(a)
    op.SetB(b)
    return op.Result()
}

func TestOperator(t *testing.T) {
    var (
        factory OperatorFactory
    )

    factory = PlusOperatorFactory{}
    if compute(factory, 1, 2) != 3 {
        t.Fatal("error with factory method pattern")
    }

    factory = MinusOperatorFactory{}
    if compute(factory, 4, 2) != 2 {
        t.Fatal("error with factory method pattern")
    }
}

相关文章

  • 【Go 精选】基本语法 - 继承和接口

    1.如何利用结构体实现继承 Dog 继承 Pet,重写函数 ShowB() 2.如何使用接口 ① 不需要显式声明实...

  • go语言学习(8)--Struct

    面向对象 go只支持封装,不支持继承和多态go是面向接口的编程,也可以说go所有对象都是继承了一个空接口java这...

  • kotlin继承

    继承语法 类和接口的继承通过 : 来实现 接口 kotlin 的接口可以包含抽象方法,以及方法的实现,接口可以有属...

  • JAVA基础概念

    整体目录 面向对象概念 JAVA基本语法 JAVA类 继承与接口 Numbers And Strings Gene...

  • 第04天(面对对象编程)_04

    16_接口的继承.go 17_接口转换.go 18_空接口.go 19_类型断言:if.go 20_类型断言:sw...

  • beego 模版语法

    beego 模板语法指南 本文讲述 beego 中使用的模板语法,与 go 模板语法基本相同。 基本语法 go 统...

  • 【Golang 基础】Go 语言 面向对象

    Go 语言的面向对象   Go 语言的面向对象非常简单,仅支持封装,不支持继承和多态。继承和多态是在接口中实现的。...

  • Python 之 面向对象继承

    基本继承语法 多继承

  • Go语言之Interface(二)

    使用指针接收器和值接收器实现接口 实现多个接口 接口嵌套 在Go语言中没有继承的概念,但是通过组合可以实现继承的效...

  • C#魔帅-lesson_04-接口

    接口(Interface) 接口定义了所有类继承接口时应遵循的语法合同。接口定义了语法合同 "是什么" 部分,派生...

网友评论

      本文标题:【Go 精选】基本语法 - 继承和接口

      本文链接:https://www.haomeiwen.com/subject/czabqdtx.html