原文: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. 玩转CONSUL(1)–WATCH机制探究

    1. 前言 consul 经常被用于服务的注册和发现,本文将带你对watch做更深入的探究 2. consul对外暴露了4种通讯接口 2.1 RPC 主要用于内部通讯Gossip/日志分发/选主等 2 ...

  2. Spring Boot 初学避免犯不必要的错误

    创建项目时的目录问题: 新包体(例如controller)必须和启动文件 DemoApplication.java 在同一级目录下,如下 spring boot 初步使用创建新的项目:https:// ...

  3. Appium移动自动化测试-----(六)3.AppiumDesktop功能描述

    一般功能 这些能力跨越多个驱动因素. 能力 描述 值 automationName 使用哪个自动化引擎 Appium(默认)或Selendroid或者UiAutomator2或者Espresso对于A ...

  4. twemproxy配置

    redis多主从,多节点,读写分离架构. nutcracker.yml的twemproxy配置 #redis_main是twemproxy所控制redis主从集群逻辑名称 redis_main: #t ...

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

    表单 Flask-WTF 项目结构 pip install flask-wtf 为了实现CSRF 保护,Flask-WTF 需要程序设置一个密钥.Flask-WTF 使用这个密钥生成加密令牌,再用令牌 ...

  6. 20191031:GIL全局解释锁

    20191031:GIL全局解释锁 总结关于GIL全局解释锁的个人理解 GIl全局解释锁,本身不是Python语言的特性,而是Python语言底层的c Python解释器的一个特性.在其他解释器中是没 ...

  7. T-SQL学习笔记

    学习T-SQL时记录的笔记,记得并不全也不详细 if和while语句 declare @age int select @age = DATEDIFF(year,stuAge,getdate()) fr ...

  8. Scala 数组操作之数组转换

    使用yield和函数式编程转换数组 // 对Array进行转换,获取的还是Array val a = Array(1, 2, 3, 4, 5) val a2 = for (ele <- a) y ...

  9. Vue组件全局/局部注册

    全局注册 main.js中创建 Vue.component('button-counter', { data: function () { return { count: 0 } }, templat ...

  10. [UOJ#404][CTSC2018]组合数问题(79分,提交答案题,模拟退火+匈牙利+DP)

    1.4.5.6.10都是op=1的点,除4外直接通过模拟退火调参可以全部通过. #include<cmath> #include<ctime> #include<cstd ...