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 ...
随机推荐
- ip网络
- LVS-TUN模式
TUN模式: 其实数据转发原理和上图是一样的,不过这个我个人认为主要是位于不同位置(不同机房):LB是通过隧道进行了信息传输,虽然增加了负载,可是因为地理位置不同的优势,还是可以参考的一种方案: 优点 ...
- 在ensp上配置通过Stelnet登录系统
我们为什么我们要采用Stelent登录? 因为不安全,我们要采用更加安全的方式,双向加密,通过ssh网络安全协议 下面我们开始实验:使用路由器R1模拟PC,作为SSH的客户端:路由器R2作为SSH的服 ...
- 【作业】Kitchen Plates(拓扑排序)
题目链接:https://vjudge.net/contest/345791#problem/O [问题描述] You are given 5 different sizes of kitchen p ...
- 字符串的分隔方法 split()
java中的split()的方法 string.split([separator,[limit]]) 参数 string (必选),要被分解的 String 对象或文字.该对象不会被 split 方法 ...
- java当中JDBC当中Scrollable和Updatable ResultSet的用法和Helloworld例子
[学习笔记] 在前面的jdbc的Helloworld程序当中,我们接触了最简单的 Statement.那种Statement的光标只能向前移.意思就是访问完2,只能继续访问3,不能再回过头来访问1.还 ...
- 题解 Luogu P1099 【树网的核】
这题是真的水啊... ------------ 昨天模拟赛考了这题,很多人都是O($n^3$)水过,但我认为,要做就做的足够好(其实是我根本没想到O($n^3$)的做法),然后就开始想O(n)的解法. ...
- Windows环境下Python3安装Pyspider
执行命令: pip3 install pyspider Windows 下可能会出现这样的错误提示:Command "python setup.py egg_info" fai ...
- PAT甲级满分有感
时间轴: 2017年,数据结构加入了我的课程清单. 2018年12月,我从网易云课堂下载了数据结构的所有课程视频(学校里没有网,只能离线看),开始一刷.一刷只看了视频,基本没有做题,看到AVL树的时候 ...
- 【HC89S003F4开发板】 8c转义成汇编工程
HC89S003F4开发板建立汇编工程 选择编译文件 @选用开发板闪灯例程,将例程删除多余的注释,后面生成的文件会更直观. #define ALLOCATE_EXTERN #include " ...