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. API gateway 之 kong 安装

    kong安装: https://getkong.org/install/centos/ 下载指定版本rpm: wget https://bintray.com/kong/kong-community- ...

  2. linux利用scp远程上传下载文件/文件夹

    scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速度. 当你服务 ...

  3. Android几种解析XML方式的比较

    https://blog.csdn.net/isee361820238/article/details/52371342 一.使用SAX解析XML SAX(Simple API for XML) 使用 ...

  4. Jquery autocomplete.js输入框联想补全功能

    Jquery autocomplete.js插件下载地址:http://files.cnblogs.com/files/jinzhiming/autocomplete.rar 有两种用法,一种是直接使 ...

  5. Java中五种遍历HashMap的方式

    import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Java8Templat ...

  6. Apache正向代理和反向代理

    一.正向代理 先说一正向代理(Forward Proxy),通常普通用户使用的比较多的,是正向代理.也就是在浏览器的网络连接属性框中,填写上一个代理服务器的ip和端口,即可通过代理服务器中转,去浏览网 ...

  7. 判断一个点是否在RotatedRect中

    openCV函数pointPolygonTest(): C++: double pointPolygonTest(InputArray contour, Point2f pt, bool measur ...

  8. 回归Android之Android基础和小常识

    Activity ,Service,Content Provider,BroadcastReceiver, Intent SQLite,Http,Fragement,Handle 1,Activity ...

  9. Oracle使用——oracle表锁住,杀掉锁表进程

    背景 在操作Oracle时,多人同时操作oracle数据库的同一张表的时候,经常会造成锁表现象,这时需要手动进行解锁. 步骤 以dba身份登录Oracle数据库(否则用户缺少杀掉进程权限,需要给用户分 ...

  10. Flask学习【第10篇】:自定义Form组件

    wtforms源码流程 实例化流程分析 1 # 源码流程 2 1. 执行type的 __call__ 方法,读取字段到静态字段 cls._unbound_fields 中: meta类读取到cls._ ...