Learn Golang in Days - Day 14

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

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()

}

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

查看所有标签

猜你喜欢:

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

Programming Collective Intelligence

Programming Collective Intelligence

Toby Segaran / O'Reilly Media / 2007-8-26 / USD 39.99

Want to tap the power behind search rankings, product recommendations, social bookmarking, and online matchmaking? This fascinating book demonstrates how you can build Web 2.0 applications to mine the......一起来看看 《Programming Collective Intelligence》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

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

UNIX 时间戳转换