原文: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.

Multiple inheritance in Go的更多相关文章

  1. Multiple Inheritance in C++

    Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. The c ...

  2. 面向对象程序设计-C++ Inheritance & Multiple inheritance & RTTI【第十三次上课笔记】

    Sadly, 这节课带过去的笔记本没电了 T^T 导致没有一行 Code, Sorry 笔记如下: Shape * p1; //使用指针创建对象的方法 p = new Circle (2.0); Sh ...

  3. 条款40:明智而审慎地使用多重继承(use multiple inheritance judiciously)

    NOTE: 1.多重继承比单一继承复杂.它可能导致新的歧义性,以及对virtual继承的需要. 2.virtual 继承会增加大小 速度 初始化(及赋值)复杂度等等成本.如果virtual base ...

  4. Memory Layout for Multiple and Virtual Inheritance

    Memory Layout for Multiple and Virtual Inheritance(By Edsko de Vries, January 2006)Warning. This art ...

  5. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

    // the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...

  6. [置顶] c++类的继承(inheritance)

    在C++中,所谓"继承"就是在一个已存在的类的基础上建立一个新的类.已存在的类(例如"马")称为"基类(base class )"或&quo ...

  7. Classical Inheritance in JavaScript

    JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance in ...

  8. (转) Friendship and inheritance

    原地址: http://www.cplusplus.com/doc/tutorial/inheritance/ Friend functions In principle, private and p ...

  9. <Effective C++>读书摘要--Inheritance and Object-Oriented Design<二>

    <Item 36> Never redefine an inherited non-virtual function 1.如下代码通过不同指针调用同一个对象的同一个函数会产生不同的行为Th ...

随机推荐

  1. python:datetime.datetime is not JSON serializable 报错问题解决

    问题: 项目使用django开发,返回的数据中有时间字段,当json.dumps()时提示:datetime.datetime is not JSON serializable 解决: import ...

  2. Django 之上下文处理器和中间件

    一.上下文处理器 上下文处理器是可以返回一些数据,在全局模板中都可以使用.比如登录后的用户信息,在很多页面中都需要使用,那么我们可以放在上下文处理器中,就没有必要在每个视图函数中都返回这个对象. 在s ...

  3. PHP中NULL和‘'的区别

    PHP中NULL和‘'区别 null的类型是null,""的类型是string 所以是不同东西 <pre>$a=22;unset($a);var_dump($a);&l ...

  4. Hue,Oozie运行sqoop找不到驱动解决办法

    一.前言 平台:CDH 5.13.0 公司在客户那边有个项目需要部署cloudera平台,部署的时候,在这个版本的cdh中,除了基本组件,还选了sqoop2作为数据传输工具,希望能在Oozie中通过工 ...

  5. 从零开始学Flask框架-002

    Jinja2模板 默认情况下,Flask 在程序文件夹中的templates 子文件夹中寻找模板. Jinja2 中的extends 指令从Flask-Bootstrap 中导入bootstrap/b ...

  6. vue 下拉刷新数据的插件的使用:

    1.安装: npm i vue-scroller -S npm install vue-scroller -D 2.在需要加载的页面中引入,或在公共js文件中引入: import VueScrolle ...

  7. Java8新特性 - 并行流与串行流

    并行流就是把一个内容分成多个数据块,并用不同的线程分别处理每个数据块的流. Java8中将并行进行了优化,我们可以很容易的对数据进行并行操作.Stream API可以声明性地通过parallel()和 ...

  8. VS.NET(C#)--2.3良构的XHTML

    良构的XHTML 1.关闭所有标签 2.禁止标签嵌套 3.区分大小写 4.引号  所有属性值都要置于引号中 5.唯一的根元素<html></html> 6.保留字符 XML中五 ...

  9. CTR预估-GBDT与LR实现

    1.来源 本质上 GBDT+LR 是一种具有 stacking 思想的二分类器模型,所以可以用来解决二分类问题.这个方法出自于 Facebook 2014 年的论文 Practical Lessons ...

  10. Vue的学习笔记

    以下文章皆为观看慕课网https://www.imooc.com/learn/796中“河畔一角”老师的讲解做的笔记,仅供参考. 一.Vue特点 Vue是MVVM的框架,也就是模型视图->视图模 ...