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>

转载于:https://www.cnblogs.com/sea-stream/p/10357586.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值