相关疑难解决方法(0)

Go中是否存在脆弱的基类问题?

尽管使用组合而不是继承?

如果是这样,语言层面是否有任何解决方案?

oop inheritance composition go

5
推荐指数
1
解决办法
155
查看次数

转换嵌入式struct call子方法代替父方法

这里是Go代码示例,包含Interface,Parent Struct和2 Children Structs

package main

import (
    "fmt"
    "math"
)

// Shape Interface : defines methods
type ShapeInterface interface {
    Area() float64
    GetName() string
    PrintArea()
}

// Shape Struct : standard shape with an area equal to 0.0
type Shape struct {
    name string
}

func (s *Shape) Area() float64 {
    return 0.0
}

func (s *Shape) GetName() string {
    return s.name
}

func (s *Shape) PrintArea() {
    fmt.Printf("%s : Area %v\r\n", s.name, s.Area())
}

// Rectangle Struct : …
Run Code Online (Sandbox Code Playgroud)

methods struct parent-child go

4
推荐指数
1
解决办法
848
查看次数

标签 统计

go ×2

composition ×1

inheritance ×1

methods ×1

oop ×1

parent-child ×1

struct ×1