duck typing

Python 

def download(retriever):

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

C++

template <class R>

string download(const R& retriever){

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

}

Java

<R extends Retriever>

String download(R r){

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

}

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

Go:传入的参数必须实现Retriever接口,非duck typing

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

}

 

posted @ 2021-07-03 09:56  猫七的blog  阅读(34)  评论(0编辑  收藏  举报