在特定情况下,采用 Go 继承的最佳实践方式是什么?
时间:2024-03-10
浏览:484
作者:佚名
IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《在特定情况下,采用 Go 继承的最佳实践方式是什么?》,聊聊,我们一起来看看吧!
我一直在思考这个特定的问题,我正在考虑如何以最干净的方式解决它。
想象一个应用程序如下所示:
type areacalculator interface {
area() int
}
type rectangle struct {
color string
width int
height int
}
type (r *rectangle) area() int {
return r.width * r.height
}
type circle struct {
color string
diameter int
}
type (c *circle) area() int {
return r.diameter / 2 * r.diameter / 2 * π
}
type canvas struct {
children []areacalculator
}
func (c *canvas) string() {
for child := range c.children {
fmt.println("area of child with color ", child.color, " ", child.area())
}
}
这个例子显然无法编译,因为虽然 canvas 的 string() 方法可以调用 c.area(),但它无法访问 c.color,因为无法确保实现 areacalculator 的结构具有该属性。
我能想到的一个解决方案是这样做:
type areacalculator interface {
area() int
color() string
}
type rectangle struct {
color string
width int
height int
}
type (r *rectangle) color() string {
return r.color
}
type (r *rectangle) area() int {
return r.width * r.height
}
type circle struct {
color string
diameter int
}
type (c *circle) area() int {
return r.diameter / 2 * r.diameter / 2 * π
}
type (c *circle) color() string {
return c.color
}
type canvas struct {
children []areacalculator
}
func (c *canvas) string() {
for child := range c.children {
fmt.println("area of child with color ", child.color(), " ", child.area())
}
}
另一种方法是尝试这样的事情:
type Shape struct {
Area func() int
color string
diameter int
width int
height int
}
func NewCircle() Shape {
// Shape initialisation to represent a Circle. Setting Area func here
}
func NewRectangle() Shape {
// Shape initialisation to represent a Rectangle. Setting Area func here
}
type Canvas struct {
children []Shape
}
func (c *Canvas) String() {
for child := range c.children {
fmt.Println("Area of child with color", child.color, " ", child.Area())
}
}
这些选项对我来说都不干净。我确信有一种我想不出的更清洁的解决方案。
解决方案
一个重要的起点是你不应该模仿 go 中的继承。 go 没有继承。它有接口并且有嵌入。他们没有忘记包括继承; it's intentionally not part of the language.go 鼓励组合。
您的 canvas 需要的不仅仅是 areacalculator。它需要提供颜色的东西。你需要表达这一点。例如,您可以这样做:
type drawableshape interface {
areacalculator
color() string
}
然后您将为 rectangle 和 circle 实现 color()。
func (r rectangle) color() string {
return r.color
}
func (c circle) color() string {
return c.color
}
children 将是 []drawableshape:
children []drawableshape
这将留下类似 this 的内容(基于 mohammad nasirifar 的代码构建)。
package main
import (
"fmt"
"math"
"strings"
)
type areacalculator interface {
area() int
}
type drawableshape interface {
areacalculator
color() string
}
type rectangle struct {
color string
width int
height int
}
func (r rectangle) area() int {
return r.width * r.height
}
func (r rectangle) color() string {
return r.color
}
type circle struct {
color string
diameter int
}
func (c circle) area() int {
area := math.round(float64(c.diameter*c.diameter) * math.pi / float64(4))
return int(area)
}
func (c circle) color() string {
return c.color
}
type canvas struct {
children []drawableshape
}
func (c canvas) string() string {
lines := make([]string, 0)
for _, child := range c.children {
lines = append(lines, fmt.sprintf("area of child with color %s %d", child.color(), child.area()))
}
return strings.join(lines, "\n")
}
func main() {
circle := &circle{color: "red", diameter: 2}
rect := &rectangle{color: "blue", width: 3, height: 4}
canvas := &canvas{
children: []drawableshape{circle, rect},
}
fmt.println(canvas.string())
}这里的关键观察是,如果您需要特定功能,请将其明确化。也不要代表其他对象执行其他对象的工作。
另请注意,string() 必须返回字符串,而不是写入 stdout。
package main
import (
"fmt"
"math"
"strings"
)
type AreaCalculator interface {
fmt.Stringer
Area() int
}
type Rectangle struct {
color string
width int
height int
}
func (r *Rectangle) Area() int {
return r.width * r.height
}
func (r *Rectangle) String() string {
return fmt.Sprintf("I'm a rectangle %d", r.width)
}
type Circle struct {
color string
diameter int
}
func (c *Circle) Area() int {
area := math.Round(float64(c.diameter*c.diameter) * math.Pi / float64(4))
return int(area)
}
func (c *Circle) String() string {
return fmt.Sprintf("I'm a circle: %d", c.diameter)
}
type Canvas struct {
children []AreaCalculator
}
func (c *Canvas) String() string {
lines := make([]string, 0)
for _, child := range c.children {
lines = append(lines, child.String())
}
return strings.Join(lines, "\n")
}
func main() {
circle := &Circle{color: "red", diameter: 2}
rect := &Rectangle{color: "blue", width: 3, height: 4}
canvas := &Canvas{
children: []AreaCalculator{circle, rect},
}
fmt.Println(canvas.String())
}好了,本文到此结束,带大家了解了《在特定情况下,采用 Go 继承的最佳实践方式是什么?》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
站内热门
最新文章
热门专题
