zoukankan      html  css  js  c++  java
  • 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>
    ",
            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>
    ",
            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!
    ")
    }
    
    // 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>
    ",
            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>
  • 相关阅读:
    利用VS的预生成事件获取SVN版本作为项目内部版本号
    静态构造函数到底执行了多少次?
    C#之自定义的implicit和explicit转换
    FineUI之使用SQL脚本从数据库表中生成相应的输入控件
    文件操作
    PHP中文件类型 文件属性 路径以及 文件相关的函数
    MySqli 中预处理类 stmt
    MySql 事务处理
    MySqli 执行多条SQL语句
    PHP与MySqli
  • 原文地址:https://www.cnblogs.com/sea-stream/p/10357586.html
Copyright © 2011-2022 走看看