zoukankan      html  css  js  c++  java
  • go 接口以及对象的使用

    // Sample program to show how to declare methods and how the Go
    // compiler supports them.
    package main
    
    import (
        "fmt"
    )
    
    // user defines a user in the program.
    type user struct {
        name  string
        email string
    }
    
    // notify implements a method with a value receiver.
    func (u user) notify() {
        fmt.Printf("Sending User Email To %s<%s>
    ",
            u.name,
            u.email)
    }
    
    // changeEmail implements a method with a pointer receiver.
    func (u *user) changeEmail(email string) {
        fmt.Printf("change Email from <%s> To <%s>
    ", u.email, email)
        u.email = email
    }
    
    // main is the entry point for the application.
    func main() {
        // Values of type user can be used to call methods
        // declared with a value receiver.
        bill := user{"Bill", "bill@email.com"}
        bill.notify()
    
        // Pointers of type user can also be used to call methods
        // declared with a value receiver.
        lisa := &user{"Lisa", "lisa@email.com"}
        lisa.notify()
        fmt.Println()
        // Values of type user can be used to call methods
        // declared with a pointer receiver.
        bill.changeEmail("bill@newdomain.com")
        bill.notify()
    
        // Pointers of type user can be used to call methods
        // declared with a pointer receiver.
        lisa.changeEmail("lisa@newdomain.com")
        lisa.notify()
    }

    输出

    Sending User Email To Bill<bill@email.com>
    Sending User Email To Lisa<lisa@email.com>
    
    change Email from <bill@email.com> To <bill@newdomain.com>
    Sending User Email To Bill<bill@newdomain.com>
    change Email from <lisa@email.com> To <lisa@newdomain.com>
    Sending User Email To Lisa<lisa@newdomain.com>
  • 相关阅读:
    [转]理解java的三大特性之多态
    [转]java:IO流学习小结
    Base64 加密之中文乱码
    piwik优化之定时任务生成统计数据
    php统计中英文混合的文章字数
    Linux常用命令之定时任务
    skype在线状态代码详解
    php+google/baidu翻译接口
    php限制文件下载速度的代码
    PHP破解wifi密码(wifi万能钥匙的接口)
  • 原文地址:https://www.cnblogs.com/sea-stream/p/10357492.html
Copyright © 2011-2022 走看看