在特定情况下,采用 Go 继承的最佳实践方式是什么?
来源:stackoverflow
时间:2024-03-10 18:30:27 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知识!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
478 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习