原文: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. 管道式编程(Pipeline Style programming)

    受 F# 中的管道运算符和 C# 中的 LINQ 语法,管道式编程为 C# 提供了更加灵活性的功能性编程.通过使用 扩展函数 可以将多个功能连接起来构建成一个管道. 前言 在 C# 编程中,管道式编程 ...

  2. SGE基本操作

    SGE怎样工作: 1. 接受用户投放的任务 2. 在任务运行以前,将任务放到一个存储区域 3. 发送任务到一个执行设备,并监控任务的运行 4. 运行结束写回结果并记录运行日志 常用的SGE命令: 1. ...

  3. mac 上更改环境变量

    第一次配置Mac的环境变量,到网上转了一圈才找到正确方法. 打开终端,新建.bash_profile文件在~/目录下(如果电脑里已经有了这个文件,跳过这一步) touch ~/.bash_profil ...

  4. 软件素材--c/c++干掉代码的通用方法

    while(1) { sleep(200); } #endif

  5. 怎么对10亿数据量级的mongoDB作高效的全表扫描

    转自:http://quentinxxz.iteye.com/blog/2149440 一.正常情况下,不应该有这种需求 首先,大家应该有个概念,标题中的这个问题,在大多情况下是一个伪命题,不应该被提 ...

  6. PAT(B) 1087 有多少不同的值(Java)规律

    题目链接:1087 有多少不同的值 (20 point(s)) 题目描述 当自然数 n 依次取 1.2.3.--.N 时,算式 ⌊n/2⌋+⌊n/3⌋+⌊n/5⌋ 有多少个不同的值?(注:⌊x⌋ 为取 ...

  7. WUSTOJ 1332: Prime Factors(Java)

    题目链接:1332: Prime Factors Description I'll give you a number , please tell me how many different prim ...

  8. WUSTOJ 1320: 饭卡(Java)动态规划-背包

    题目链接:

  9. 路由基础(Routing)

    查看本机路由表: [root@controller02 ~]# cat /etc/iproute2/rt_tables # # reserved values # 255     local 254  ...

  10. Self寄宿

    static void Main(string[] args) { //Assembly.Load("WebApplication1, Version=1.0.0.0, Culture=ne ...