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
}
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.
Multiple inheritance in Go的更多相关文章
- Multiple Inheritance in C++
Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. The c ...
- 面向对象程序设计-C++ Inheritance & Multiple inheritance & RTTI【第十三次上课笔记】
Sadly, 这节课带过去的笔记本没电了 T^T 导致没有一行 Code, Sorry 笔记如下: Shape * p1; //使用指针创建对象的方法 p = new Circle (2.0); Sh ...
- 条款40:明智而审慎地使用多重继承(use multiple inheritance judiciously)
NOTE: 1.多重继承比单一继承复杂.它可能导致新的歧义性,以及对virtual继承的需要. 2.virtual 继承会增加大小 速度 初始化(及赋值)复杂度等等成本.如果virtual base ...
- Memory Layout for Multiple and Virtual Inheritance
Memory Layout for Multiple and Virtual Inheritance(By Edsko de Vries, January 2006)Warning. This art ...
- JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance
// the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...
- [置顶] c++类的继承(inheritance)
在C++中,所谓"继承"就是在一个已存在的类的基础上建立一个新的类.已存在的类(例如"马")称为"基类(base class )"或&quo ...
- Classical Inheritance in JavaScript
JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance in ...
- (转) Friendship and inheritance
原地址: http://www.cplusplus.com/doc/tutorial/inheritance/ Friend functions In principle, private and p ...
- <Effective C++>读书摘要--Inheritance and Object-Oriented Design<二>
<Item 36> Never redefine an inherited non-virtual function 1.如下代码通过不同指针调用同一个对象的同一个函数会产生不同的行为Th ...
随机推荐
- 2019年Java面试题基础系列228道,题目汇总,可以先看会多少
Java面试题(一) 1.面向对象的特征有哪些方面? 2.访问修饰符 public,private,protected,以及不写(默认)时的区别? 3.String 是最基本的数据类型吗? 4.flo ...
- JIRA数据库的迁移,从HSQL到MYSQL/Oracle
Jira数据库迁移,从HSQL到MYSQL 通过JIRA管理员登录,进入“管理员页面”,“系统”--“导入&导出”,以XML格式备份数据. 在MySQL中创建Schema,命名为jira 关闭 ...
- Ubuntu中Qt Creator无法启动调试
Ubuntu下安装Qt creator后无法启动调试,报错为Ptrace:Operation not permitted. 产生原因: 在Ubuntu 11.04("Natty Narwha ...
- kafka那些事儿
1 为什么用消息队列 1)解耦.服务之间没有强依赖,不需要关心调用服务时出现的各种异常,服务挂掉后接口超时等问题 2)异步.解决接口调用多服务时延时高的问题 3)高峰期服务间缓冲.解决工作节奏不一致问 ...
- [SourceTree] - 使用内置 PuTTY 克隆项目出现 fatal: early EOF 问题之解决
背景 使用 PuTTY 克隆 Asp.Net Core 项目失败. 错误 git -c filter.lfs.smudge= -c filter.lfs.required=false -c diff. ...
- [SourceTree] - 使用内置 Git 克隆项目出现 templates not found 问题之解决
背景 使用 SourceTree 克隆 Asp.Net Core 项目失败. 错误 warning: templates not found C:\Program Files\Git\share\gi ...
- notepad++安装markdown
notepad++ 安装markdown安装markdown插件一.下载最新的markdown插件, github:https://github.com/nea/MarkdownViewerPlusP ...
- PAT(B) 1034 有理数四则运算(Java)
题目链接:1034 有理数四则运算 (20 point(s)) 题目描述 本题要求编写程序,计算 2 个有理数的和.差.积.商. 输入格式 输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数 ...
- PAT甲级题分类汇编——排序
本文为PAT甲级分类汇编系列文章. 排序题,就是以排序算法为主的题.纯排序,用 std::sort 就能解决的那种,20分都算不上,只能放在乙级,甲级的排序题要么是排序的规则复杂,要么是排完序还要做点 ...
- 准备写个Golang开发的教程
进入golang的开发已经差不多两年了,最近打算写个Golang的教程.目的是让有开发基础,没接触过Golang的人能够愉快高效地写出Golang项目. 1 记得17年底时候,有个特别小的项目,准备试 ...