interface - 接口(interface)的方法

标签 interface methods go

下一个实现附加到接口(interface)的方法的方法是否正确? (getKey, getData)

type reader interface {
    getKey(ver uint) string
    getData() string
}

type location struct {
    reader
    fileLocation string
    err os.Error
}

func (self *location) getKey(ver uint) string {...}

func (self *location) getData() string {...}

func NewReader(fileLocation string) *location {
    _location := new(location)
    _location.fileLocation = fileLocation
    return _location
}

最佳答案

在 Go 中,你不需要明确地说你正在实现一个接口(interface)——如果一个类型具有接口(interface)所需的一切,它就可以通过该接口(interface)使用。所以你不需要在 type location struct 中说 reader

参见此处:http://golang.org/doc/effective_go.html#interfaces_and_types

关于interface - 接口(interface)的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2841901/

相关文章:

java - 实现接口(interface)时出现错误 "Cannot reduce the visibility of a inherited method"

java - 实现Comparable接口(interface)的问题

java - 为什么java不让我调用这个 protected 函数

java - 为什么 Eclipse 建议我将我的方法设为静态

来自 x509 证书的 golang 主题 DN

java - 将数据分块同步到服务器会导致 StackoverflowException

linux - 模拟进入 Linux 接口(interface)的数据包

java - 如何使用方法解决: Locking account after three times?

go - 返回消息列表

go - golang 中的词法范围?