demo1

// Sample program to show how polymorphic behavior with interfaces.
package main import (
"fmt"
) // notifier is an interface that defines notification
// type behavior.
type notifier interface {
notify()
} // user defines a user in the program.
type user struct {
name string
email string
} // notify implements the notifier interface with a pointer receiver.
func (u *user) notify() {
fmt.Printf("Sending user email to %s<%s>\n",
u.name,
u.email)
} // admin defines a admin in the program.
type admin struct {
name string
email string
} // notify implements the notifier interface with a pointer receiver.
func (a *admin) notify() {
fmt.Printf("Sending admin email to %s<%s>\n",
a.name,
a.email)
} // main is the entry point for the application.
func main() {
// Create a user value and pass it to sendNotification.
bill := user{"Bill", "bill@email.com"}
sendNotification(&bill) // Create an admin value and pass it to sendNotification.
lisa := admin{"Lisa", "lisa@email.com"}
sendNotification(&lisa)
} // sendNotification accepts values that implement the notifier
// interface and sends notifications.
func sendNotification(n notifier) {
n.notify()
}

输出

Sending user email to Bill<bill@email.com>
Sending admin email to Lisa<lisa@email.com>

demo2

// Sample program to show what happens when the outer and inner
// type implement the same interface.
package main import (
"fmt"
) // notifier is an interface that defined notification
// type behavior.
type notifier interface {
notify()
} // user defines a user in the program.
type user struct {
name string
email string
} // notify implements a method that can be called via
// a value of type user.
func (u *user) notify() {
fmt.Printf("Sending user email to %s<%s>\n",
u.name,
u.email)
} // admin represents an admin user with privileges.
type admin struct {
user
level string
} // notify implements a method that can be called via
// a value of type Admin.
func (a *admin) notify() {
fmt.Printf("Sending admin email to %s<%s>\n",
a.user.name,
a.user.email)
} // main is the entry point for the application.
func main() {
// Create an admin user.
ad := admin{
user: user{
name: "john smith",
email: "john@yahoo.com",
},
level: "super",
} // Send the admin user a notification.
// The embedded inner type's implementation of the
// interface is NOT "promoted" to the outer type.
sendNotification(&ad) // We can access the inner type's method directly.
ad.user.notify() // The inner type's method is NOT promoted.
ad.notify()
} // sendNotification accepts values that implement the notifier
// interface and sends notifications.
func sendNotification(n notifier) {
n.notify()
}

输出

Sending admin email to john smith<john@yahoo.com>
Sending user email to john smith<john@yahoo.com>
Sending admin email to john smith<john@yahoo.com>

demo3

// Sample program to show what happens when the outer and inner
// type implement the same interface.
package main import (
"fmt"
) // notifier is an interface that defined notification
// type behavior.
type notifier interface {
notify()
} // user defines a user in the program.
type user struct {
name string
email string
} // notify implements a method that can be called via
// a value of type user.
func (u *user) notify() {
fmt.Printf("Sending user email to %s<%s>\n",
u.name,
u.email)
} // admin represents an admin user with privileges.
type admin struct {
user
level string
} // notify implements a method that can be called via
// a value of type Admin.
func (a *admin) notify() {
fmt.Printf("Sending admin email to %s<%s>\n",
a.name,
a.email)
} // main is the entry point for the application.
func main() {
// Create an admin user.
ad := admin{
user: user{
name: "john smith",
email: "john@yahoo.com",
},
level: "super",
} // Send the admin user a notification.
// The embedded inner type's implementation of the
// interface is NOT "promoted" to the outer type.
sendNotification(&ad) // We can access the inner type's method directly.
ad.user.notify() // The inner type's method is NOT promoted.
ad.notify()
} // sendNotification accepts values that implement the notifier
// interface and sends notifications.
func sendNotification(n notifier) {
n.notify()
}

输出

Sending admin email to john smith<john@yahoo.com>
Sending user email to john smith<john@yahoo.com>
Sending admin email to john smith<john@yahoo.com>

go 多态的更多相关文章

  1. Java中的多态

    1.多态:具有表现多种形态的能力的特征 父类: public abstract class Animal {public abstract void Say();} 子类: public class ...

  2. C# 工厂模式+虚方法(接口、抽象方法)实现多态

    面向对象语言的三大特征之一就是多态,听起来多态比较抽象,简而言之就是同一行为针对不同对象得到不同的结果,同一对象,在不同的环境下得到不同的状态. 实例说明: 业务需求:实现一个打开文件的控制台程序的d ...

  3. C#非常重要基础之多态

    前几天看了一位同志的博客,写的是关于他自己去支付宝面试的经历.过程大体是这样的:问答的时候,前面部分,作者都应答如流,说起自己经验如何之丰富,最后面试官问了作者一个问题:请简述多态的概念和作用.结果这 ...

  4. C++多态详解

    多态是面向对象的程序设计的关键技术.多态:调用同一个函数名,可以根据需要但实现不同的功能.多态体现在两个方面,我们以前学过的编译时的多态性(函数重载)和现在我们这一章将要学习的运行时的多态性(虚函数) ...

  5. 【那些年关于java多态应用】

    1.多态:具有表现多种形态的能力的特征 父类: public abstract class Animal { public abstract void Say();} 子类: public class ...

  6. JAVA多态

    多态是指当系统A访问系统B的服务时,系统B可以通过多种方式来提供服务,而这一切对系统A是透明的.比如动物园的饲养员能够给各种各样的动物喂食.下图显示了饲养员Feeder,食物Food和动物Animal ...

  7. C#多态“说来也说”——逻辑层BLL中的多态使用

    本文版权归博客园和作者吴双本人共同所有.欢迎转载,转载和爬虫请注明原文地址 http://www.cnblogs.com/tdws/p/5861842.html 昨天晚上,有个朋友说学了好久,依然没搞 ...

  8. java多态的理解

    面向对象语言中的类有三个特征,封装.继承.多态.封装与继承很好理解,那什么是多态呢? 1.什么是多态? 多态的定义:指允许不同类的对象对同一消息做出响应.即同一消息可以根据发送对象的不同而采用多种不同 ...

  9. java中如何实现多态

    复习基础知识 多态,就是重载和重写.重载发生在一个类中.重写发生在子类,意思就是子类重写父类相同名称的方法.刚学语言有的东西,不必搞得那么清楚,只有知道怎么用就行了,有的问题你要想真正把它搞得很懂,短 ...

  10. OC多态

    要点: 1.多种形态,引用的多种形态对于一个引用变量,可以指向任何类的对象.对于一个父类的引用(类与类之间有一种继承关系),可以指向子类,也可以指向本类,指向的类型不同.当通过此引用向对象发送消息,调 ...

随机推荐

  1. foreach嵌套遍历循环的问题

    在foreach嵌套循环中使用==和equals的问题 JSONArray ja1= new JSONArray(); JSONArray ja2 = new JSONArray(); JSONObj ...

  2. 怎样从外网访问内网Jupyter Notebook?

    本地安装了一个Jupyter Notebook,只能在局域网内访问,怎样从外网也能访问到本地的Jupyter Notebook呢?本文将介绍具体的实现步骤. 准备工作 安装并启动Jupyter Not ...

  3. 使用pytorch完成kaggle猫狗图像识别

    kaggle是一个为开发商和数据科学家提供举办机器学习竞赛.托管数据库.编写和分享代码的平台,在这上面有非常多的好项目.好资源可供机器学习.深度学习爱好者学习之用.碰巧最近入门了一门非常的深度学习框架 ...

  4. composer 报错:Failed to decode response: zlib_decode(): data error 解决办法

    执行命令 composer require particle/validator 报错 Failed to decode response: zlib_decode(): data error 网上推 ...

  5. fjwc2019 D4T1 循环流

    #187. 「2019冬令营提高组」循环流 假的网络流,其实是O(1)算法 手画n个图后,你会发现只要分成几种情况讨论讨论就得了. 当$a==1$时显然不存在. 当$a!=1$时 如果$n==2$,显 ...

  6. Apache2.4反向代理设置

    一.配置方向代理 1,放开虚拟主机(反向代理)模块,在httpd.conf中把下三行配置放开: LoadModule proxy_module modules/mod_proxy.so LoadMod ...

  7. 看起来像是break的考量

    代码运用了if+break: package day20181008; import java.util.Scanner; public class ZuoYe01 { public static v ...

  8. mysql "The user specified as a definer ('root'@'%') does not exist" 问题

    在重配mysql的时候碰到, 解决办法: 重新授权 grant all privileges on *.* to root@"%" identified by ".&qu ...

  9. 维护keepalived与mysql漂移脚本

    环境拓扑 chengAlived #!/bin/bash function checkModelone(){ echo "重新进行获取" wget 192.168.158.147: ...

  10. http 请求头大小写的问题

    如果是默认消息头名称,消息头格式已经固定,即便输入的大小写有误,也会给你翻译成默认的写法,如果自己定义的,会自动给你翻译成小写,所以传参数的名称都用小写字母即可,否则可能取不到值,比如encrypte ...