原文: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. 知识点-Spark小节

    Spark处理字符串日期的max和min的方式Spark处理数据存储到Hive的方式Spark处理新增列的方式map和udf.functionsSpark处理行转列pivot的使用Python 3.5 ...

  2. Can't find bundle for base name javax.servlet.LocalStrings, locale zh_CN

    启动junit4测试报错,原因是没有servlet-api.jar eclipse右键项目>>>>>>Build Path>>>>>C ...

  3. BGP 实验

    一.环境准备 1. 软件:GNS3 2. 路由:c7200 二.实验操作 实验要求: 1. 掌握 BGP 的基本配置方法. 2. 掌握如何查看 BGP 的各种配置信息. 3. 掌握基于回环口的 BGP ...

  4. python内置模块介绍(一)

     本文主要介绍模块列表如下: os sys re time datetime random shutil subprocess os模块 os.getcwd()                    ...

  5. 【转载】Maven入门实践

    Maven 确确实实是个好东西,用来管理项目显得很方便,但是如果是通过 Maven 来远程下载 JAR 包的话,我宿舍的带宽是4兆的,4个人共用,有时候用 Maven 来远程下载 JAR 包会显得很慢 ...

  6. xorm表结构操作实例

    获取数据库信息 package main import ( "fmt" _ "github.com/go-sql-driver/mysql" "git ...

  7. rabbitMQ 重试

    rabbitMQ 重试机制 spring.rabbitmq.listener.simple.retry.max-attempts=5 最大重试次数spring.rabbitmq.listener.si ...

  8. Git config 使用说明(转)

    原文:https://blog.csdn.net/gdutxiaoxu/article/details/79253737

  9. List、dictionary、hashtable、ArrayList集合

    集合的引用命名空间在 system.Collections下 1.为什么引入集合 因为数组长度是固定的,为了建立一个动态的"数组",所以引入了集合. 2.为什么引入ArrayLis ...

  10. jQuery 基础知识

    一.序言 jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后的又一个优秀的JavaScript代码库(JavaScript框架).jQuery设计的宗旨是"W ...