Learn Golang in Days - Day 14

栏目: Go · 发布时间: 5年前

Learn Golang in Days - Day 14

简介

  • Go语言提供了另外一种数据类型就是接口,它把所有具有共性的方法定义在一起,只要实现了这些方法就是实现了这个接口。
package main

import "fmt"

/* 声明接口 */
type Phone interface {
    call()
}

/* 定义结构体 */
type NokiaPhone struct {
}
/* 实现接口方法 */
func (nokiaPhone NokiaPhone) call() {
    fmt.Printf("I am nokia phone.\n")
}

/* 定义结构体 */
type IPhone struct {
}
/* 实现接口方法 */
func (iPhone IPhone) call() {
    fmt.Printf("I am iphone.\n")
}

func main() {
    var phone Phone

    phone = new(NokiaPhone)
    phone.call()

    phone = new(IPhone)
    phone.call()

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Responsive Web Design

Responsive Web Design

Ethan Marcotte / Happy Cog / 2011-6 / USD 18.00

From mobile browsers to netbooks and tablets, users are visiting your sites from an increasing array of devices and browsers. Are your designs ready? Learn how to think beyond the desktop and craft be......一起来看看 《Responsive Web Design》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换