首页 > 其他 > 详细

duck typing

时间:2021-07-03 11:10:43      阅读:27      评论:0      收藏:0      [点我收藏+]

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和python中,传入的参数什么类型都可以,只需要实现get方法

Java

<R extends Retriever>

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

}

duck typing

原文:https://www.cnblogs.com/liuguangshou123/p/14965670.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!