go 内嵌对象类型
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 内嵌对象类型的更多相关文章
- Elastic search中使用nested类型的内嵌对象
在大数据的应用环境中,往往使用反范式设计来提高读写性能. 假设我们有个类似简书的系统,系统里有文章,用户也可以对文章进行赞赏.在关系型数据库中,如果按照数据库范式设计,需要两张表:一张文章表和一张赞赏 ...
- 关于js函数解释(包括内嵌,对象等)
常用写法: function add(a,b) { return a + b; } alert(add(1,2)); // 结果 3 当我们这么定义函数的时候,函数内容会被编译(但不会立即执行,除非我 ...
- 浅谈Python内置对象类型——数字篇(附py2和py3的区别之一)
Python是一门面向对象的编程设计语言,程序中每一样东西都可以视为一个对象.Python内置对象可以分为简单类型和容器类型,简单类型主要是数值型数据,而容器类型是可以包含其他对象类型的集体,如序列. ...
- easyui datagrid columns 如何取得json 内嵌对象(many-to-one POJO class)
http://www.iteye.com/problems/44119 http://hi.baidu.com/lapson_85/item/7733586e60b08500a1cf0f8d ———— ...
- 实现type函数用于识别标准类型和内置对象类型
function type(obj){ return Object.prototype.toString.call(obj).slice(8,-1); } var t=type(new Number( ...
- Mongodb内嵌对象关联查询
db.-10-30T00:00:00Z"),"$lt":ISODate("2018-10-30T23:59:00Z")}, "equip.$ ...
- Java EE JSP内置对象及表达式语言
一.JSP内置对象 JSP根据Servlet API规范提供了一些内置对象,开发者不用事先声明就可使用标准变量来访问这些对象. JSP提供了9种内置对象: (一).request 简述: JSP编程中 ...
- python——内置对象
python的内置对象 对象类型 常量示例/用法 Number(数字) 3.14159, 1234, 999L 3+4j String(字符串) 'spam', "guido's" ...
- mongodb 多表关联处理 : 内嵌以及连接(手动引用、DBref) 、aggregate中$lookup
MongoDB与关系型数据库的建模还是有许多不同,因为MongoDB支持内嵌对象和数组类型.MongoDB建模有两种方式,一种是内嵌(Embed),另一种是连接(Link).那么何时Embed何时Li ...
随机推荐
- Java技术学习路线笔记:Maven安装和作用
Maven是一个基于项目对象模型(POM)的概念的纯java开发的开源的项目管理工具.主要用来管理java项目,进行依赖管理(jar包管理,能自动分析项目所需的依赖软件包,并到Maven仓库区下载)和 ...
- git push跳过用户名和密码认证配置教程
在使用git commit命令将修改从暂存区提交到本地版本库后,只剩下最后一步将本地版本库的分支推送到远程服务器上对应的分支了,如果不清楚版本库的构成,可以查看我的另一篇,git 仓库的基本结构. 新 ...
- java日志
http://blog.csdn.net/u013628152/article/details/43538299 http://blog.csdn.net/isea533/article/detail ...
- 安装ubuntu18.04.1
下载ubuntu:https://www.ubuntu.com/download/desktop 在虚拟机创建好ubuntu18.04.1后无法启动(选择的是linux,ubuntu64位),提示:此 ...
- python selenium chrome有界面与无界面模式
from selenium.webdriver.chrome.options import Options from selenium import webdriver # 无界面模式 def Chr ...
- Docker学习笔记之Docker的Build 原理
0x00 概述 使用 Docker 时,最常用的命令无非是 docker container 和 docker image 相关的子命令,当然最初没有管理类命令(或者说分组)的时候,最常使用的命令也无 ...
- Python3 格式化字符串
Python3 格式化字符串 在Python 3.6之前,有两种将Python表达式嵌入到字符串文本中进行格式化的主要方法:%-formatting和str.format() 一.%-formatti ...
- 静态代码检查findbugs/阿里巴巴开发规范
findbugs,基本上三类严重的bug检测出来都是比较准确的,如下: 阿里巴巴开发规范 前面两类都是比较重要的: 参考: https://blog.csdn.net/qq_27093465/arti ...
- windows模糊查询指定进程是否存在
习惯的查询 wmic process | findStr /i "**" /i 忽略大小写 我查考的链接 常用批处理命令总结3之Find和FindStr
- (12网络化部署深化下)自己动手,编写神经网络程序,解决Mnist问题,并网络化部署
网络化部署一直是我非常想做的,现在已经基本看到了门路.今天早上实验,发现在手机上的支持也非常好(对于相机的支持还差一点),证明B/S结构的框架是非常有生命力的.下一步就是要将这个过程深化.总结,并且封 ...