demo1

// Sample program to show how to embed a type into another type and
// the relationship between the inner and outer type.
package main import (
"fmt"
) // 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 // Embedded Type
level string
} // 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",
} // We can access the inner type's method directly.
ad.user.notify() // The inner type's method is promoted.
ad.notify()
}

输出

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

demo2

// Sample program to show how to embed a type into another type and
// the relationship between the inner and outer type.
package main import (
"fmt"
) // 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 // Embedded Type
level string
} func (u *admin) notify() {
fmt.Printf("admin method!\n")
} // 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",
} // We can access the inner type's method directly.
ad.user.notify() // The inner type's method is promoted.
ad.notify()
}

输出

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

demo3

// Sample program to show how embedded types work with interfaces.
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
} // 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 "promoted" to the outer type.
sendNotification(&ad)
} // sendNotification accepts values that implement the notifier
// interface and sends notifications.
func sendNotification(n notifier) {
n.notify()
}

输出

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

go 内嵌对象类型的更多相关文章

  1. Elastic search中使用nested类型的内嵌对象

    在大数据的应用环境中,往往使用反范式设计来提高读写性能. 假设我们有个类似简书的系统,系统里有文章,用户也可以对文章进行赞赏.在关系型数据库中,如果按照数据库范式设计,需要两张表:一张文章表和一张赞赏 ...

  2. 关于js函数解释(包括内嵌,对象等)

    常用写法: function add(a,b) { return a + b; } alert(add(1,2)); // 结果 3 当我们这么定义函数的时候,函数内容会被编译(但不会立即执行,除非我 ...

  3. 浅谈Python内置对象类型——数字篇(附py2和py3的区别之一)

    Python是一门面向对象的编程设计语言,程序中每一样东西都可以视为一个对象.Python内置对象可以分为简单类型和容器类型,简单类型主要是数值型数据,而容器类型是可以包含其他对象类型的集体,如序列. ...

  4. easyui datagrid columns 如何取得json 内嵌对象(many-to-one POJO class)

    http://www.iteye.com/problems/44119 http://hi.baidu.com/lapson_85/item/7733586e60b08500a1cf0f8d ———— ...

  5. 实现type函数用于识别标准类型和内置对象类型

    function type(obj){ return Object.prototype.toString.call(obj).slice(8,-1); } var t=type(new Number( ...

  6. Mongodb内嵌对象关联查询

    db.-10-30T00:00:00Z"),"$lt":ISODate("2018-10-30T23:59:00Z")}, "equip.$ ...

  7. Java EE JSP内置对象及表达式语言

    一.JSP内置对象 JSP根据Servlet API规范提供了一些内置对象,开发者不用事先声明就可使用标准变量来访问这些对象. JSP提供了9种内置对象: (一).request 简述: JSP编程中 ...

  8. python——内置对象

    python的内置对象 对象类型 常量示例/用法 Number(数字) 3.14159, 1234, 999L 3+4j String(字符串) 'spam', "guido's" ...

  9. mongodb 多表关联处理 : 内嵌以及连接(手动引用、DBref) 、aggregate中$lookup

    MongoDB与关系型数据库的建模还是有许多不同,因为MongoDB支持内嵌对象和数组类型.MongoDB建模有两种方式,一种是内嵌(Embed),另一种是连接(Link).那么何时Embed何时Li ...

随机推荐

  1. prufer编码 cayley定理

    背景(在codeforces 917D 报废后,看题解时听闻了这两个玩意儿.实际上917D与之“木有关西”,也可以认为是利用了prufer的一些思路.) 一棵标号树的Pufer编码规则如下:找到标号最 ...

  2. javax.servlet.ServletException: Servlet.init() for servlet springmvc threw exception

    type Exception report message Servlet.init() for servlet springmvc threw exception description The s ...

  3. 安装PHP扩展32位与64位的误区(x86与x64的查看)

    在安装PHP扩展(DLL,SO),除了需要对应的PHP版本外,在WINDOWS还需要区分(TS线程,NTS非线程),如何判断呢? 1.如何判断是NTS还是TS(WINDOWS用户) 看PHP所在目录中 ...

  4. 通过经纬度获取所属城市信息-php

    测试经纬度信息,37.863036,113.598909.通过地图查询,所在城市为:阳泉. <?php class test{ public static $test_key = 'dfgfdg ...

  5. 【题解】Luogu SP8791 DYNALCA - Dynamic LCA

    原题传送门 这题用Link-Cut-Tree解决,Link-Cut-Tree详解 这道题的难点就在如何求LCA: 我们珂以先对其中一个点进行access操作,然后对另一个点进行access操作,因为L ...

  6. Angular4.x 安装|创建项目|目录结构|创建组件

    Angular4.x 安装|创建项目|目录结构|创建组件 安装最新版本的 nodejs node.js 官网:https://nodejs.org/zh-cn/ 去官网下载 node.js,下一步下一 ...

  7. [c/c++] programming之路(6)、ASCII码,数据类型、随机数、字符转换及拼接等

    一.变量 #include<stdio.h> #include<stdlib.h> void main0(){ //数据使用必须在范围内,否则产生溢出 unsigned +;/ ...

  8. iOS开发 -------- 九宫格坐标计算

    一 要求 完成下面的布局 二 分析 寻找规律,每一个UIView的x坐标和y坐标 三 实现思路 (1) 明确每一块用得是什么View; (2) 明确每个View之间的父子关系,每个视图都只有一个父视图 ...

  9. android设备如何进入深度休眠还能继续使用定时器【求解】

    经过试验,andriod设备进入深度休眠的时候,定时器是不能使用.但是阻止设备进入深度休眠,可以获取一把锁,但是拿了锁之后,设备不能进入休眠,系统的功耗会增加.怎么能够在系统进入休眠,定时器还能正常工 ...

  10. vi中如何使用cscope来查找函数的定义

    答:进入命令行模式输入:cs f g <function_name>