duck typing


Python 

def download(retriever):

  return retriever.get("www.baidu.com");

C++

template

string download(const R& retriever){

  return retriever.get("www.baidu.com");

}

 java和python中,传入的参数什么类型都可以,只需要实现get方法

Java

String download(R r){

  return r.get("www.baidu.com")

}

传入的参数必须实现Retriever接口

非duck typing

 ----------------------------------------------------------------------------------------------------------------------

GO

type Retriever interface {

  Get(url string) string

}

func download(r Retriever) string{

  return r.Get("www.baidu.com")

}

func main(){

  var r Retriever

  fmt.Println(download(r))

}

Go