好好爱自己!

Multiple inheritance in Go

原文:http://golangtutorials.blogspot.com/2011/06/multiple-inheritance-in-go.html

 

------------------------------------------------------------------------------------------------------------

Inheritance is the ability for a type to automatically obtain the behaviors of a parent class. Multiple inheritance is the ability for a type to obtain the behaviors of more than one parent class. As a real world example, if Phone was a type, then MobilePhone could be a type that inherits the behavior of the Phone type. This works in many cases, but not in all. What would happen to say a type, CameraPhone that has to have the behaviors of both a Camera and a Phone? One straightforward way to solve this would be to be able to inherit from both. (Note that in this simple example, it is possible to put a camera in a phone or a phone in a camera, but it is not always the case - say a child who inherits the behaviors or features of each of his parents.)

Some object oriented languages used to solve this by denying that multiple inheritance is ever necessary. Others work around the difficulties by providing what is called an interface and the ability for a sub type to subclass one type, but implement many interfaces. Go on the other hand has multiple inheritance. The way to get it is exactly the same way as we did for single inheritance that we already looked at, using anonymous fields. Let’s implement our Camera+Phone=CameraPhone example.

Full code

package main

import "fmt"

type Camera struct { } 

func (_ Camera) takePicture() string { //not using the type, so discard it by putting a _
    return "Click"
}

type Phone struct { } 

func (_ Phone ) call() string { //not using the type, so discard it by putting a _
    return "Ring Ring"
}

// multiple inheritance
type CameraPhone struct {
    Camera //has anonymous camera
    Phone //has anonymous phone 
}

func main() {
    cp := new (CameraPhone)  //a new camera phone instance
    fmt.Println("Our new CameraPhone exhibits multiple behaviors ...")
    fmt.Println("It can take a picture: ", cp.takePicture()) //exhibits behavior of a Camera
    fmt.Println("It can also make calls: ", cp.call()) //... and also that of a Phone
}

 

Our new CameraPhone exhibits multiple behaviors ...
It can take a picture: Click
It can also make calls: Ring Ring


In the above code, there is a Camera type and a Phone type. By having an anonymous type of each in CameraPhone, we are able to reach into the behavior of each of them as if they were direct behaviors of CameraPhone.

As you might start to notice, the number of paradigms in Go are fairly less, but they have significant extensibility on the design of the rest of the language.

posted @   立志做一个好的程序员  阅读(185)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2018-10-10 【转】6 Reasons Why JavaScript’s Async/Await Blows Promises Away (Tutorial)
2017-10-10 【转】shell中的内建命令, 函数和外部命令
2017-10-10 clear out all variables without closing terminal
2017-10-10 linux中shell命令test用法和举例
2017-10-10 解决vim粘贴时格式混乱的问题
2017-10-10 scp的两种方式
2017-10-10 source 命令的用法,是在当前bash环境下执行脚本文件

不断学习创作,与自己快乐相处

点击右上角即可分享
微信分享提示