接口的方法

问题描述:

下一个实现接口方法的方法是否正确? (getKeygetData接口的方法

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 
} 

在去,你不需要直接说你实现一个接口,如果一个类型都有一个接口所需的一切,可以通过该接口使用。所以你不需要在type location struct里面说reader

在这里看到:http://golang.org/doc/effective_go.html#interfaces_and_types

+0

事实上,不把'reader'实际上只是给''location'类型添加一个匿名成员,占用空间不做任何有用的事情? – matthias 2012-08-31 17:20:09

你已经基本上完成了它了。只要您给位置的getKey和getData方法有效的实体,*位置就会实现阅读器界面。没有必要再做任何事了。