zoukankan      html  css  js  c++  java
  • go 多态

    demo1

    // Sample program to show how polymorphic behavior with interfaces.
    package main
    
    import (
        "fmt"
    )
    
    // notifier is an interface that defines notification
    // type behavior.
    type notifier interface {
        notify()
    }
    
    // user defines a user in the program.
    type user struct {
        name  string
        email string
    }
    
    // notify implements the notifier interface with a pointer receiver.
    func (u *user) notify() {
        fmt.Printf("Sending user email to %s<%s>
    ",
            u.name,
            u.email)
    }
    
    // admin defines a admin in the program.
    type admin struct {
        name  string
        email string
    }
    
    // notify implements the notifier interface with a pointer receiver.
    func (a *admin) notify() {
        fmt.Printf("Sending admin email to %s<%s>
    ",
            a.name,
            a.email)
    }
    
    // main is the entry point for the application.
    func main() {
        // Create a user value and pass it to sendNotification.
        bill := user{"Bill", "bill@email.com"}
        sendNotification(&bill)
    
        // Create an admin value and pass it to sendNotification.
        lisa := admin{"Lisa", "lisa@email.com"}
        sendNotification(&lisa)
    }
    
    // sendNotification accepts values that implement the notifier
    // interface and sends notifications.
    func sendNotification(n notifier) {
        n.notify()
    }

    输出

    Sending user email to Bill<bill@email.com>
    Sending admin email to Lisa<lisa@email.com>

    demo2

    // Sample program to show what happens when the outer and inner
    // type implement the same interface.
    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
    }
    
    // notify implements a method that can be called via
    // a value of type Admin.
    func (a *admin) notify() {
        fmt.Printf("Sending admin email to %s<%s>
    ",
            a.user.name,
            a.user.email)
    }
    
    // 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 NOT "promoted" to the outer type.
        sendNotification(&ad)
    
        // We can access the inner type's method directly.
        ad.user.notify()
    
        // The inner type's method is NOT promoted.
        ad.notify()
    }
    
    // sendNotification accepts values that implement the notifier
    // interface and sends notifications.
    func sendNotification(n notifier) {
        n.notify()
    }

    输出

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

    demo3

    // Sample program to show what happens when the outer and inner
    // type implement the same interface.
    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
    }
    
    // notify implements a method that can be called via
    // a value of type Admin.
    func (a *admin) notify() {
        fmt.Printf("Sending admin email to %s<%s>
    ",
            a.name,
            a.email)
    }
    
    // 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 NOT "promoted" to the outer type.
        sendNotification(&ad)
    
        // We can access the inner type's method directly.
        ad.user.notify()
    
        // The inner type's method is NOT promoted.
        ad.notify()
    }
    
    // sendNotification accepts values that implement the notifier
    // interface and sends notifications.
    func sendNotification(n notifier) {
        n.notify()
    }

    输出

    Sending admin email to john smith<john@yahoo.com>
    Sending user email to john smith<john@yahoo.com>
    Sending admin email to john smith<john@yahoo.com>
  • 相关阅读:
    01.Sencha ExtJS 6
    02.Sencha ExtJS 6
    关于Jquery的delegate绑定事件无效
    细说 Form (表单)
    Rquest Request[""];Request.Form[""];Request.QueryString[""]
    一文看懂web服务器、应用服务器、web容器、反向代理服务器区别与联系
    vs 调试不进入断点
    HttpWebRequest类
    C# 利用 Windows服务模板 创建、安装与卸载Windows服务
    如何搭建win10 asp开发环境安装iis10
  • 原文地址:https://www.cnblogs.com/sea-stream/p/10357568.html
Copyright © 2011-2022 走看看