最近在使用headfirstpython一书,书中一个语法也没讲,刚刚学了c语言,所以我不是很清楚方法和函数的区别
6条回答 默认 最新
Exploring1024 2016-02-17 12:21关注主要是在面向对象的思想中,有差别:
函数function —— A series of statements which returns some value to a caller. It can also be passed zero or more arguments which may be used in the execution of the body.
方法method —— A function which is defined inside a class body. If called as an attribute of an instance of that class, the method will get the instance object as its first argument (which is usually called self).
从定义的角度上看,我们知道函数(function)就相当于一个数学公式,它理论上不与其它东西关系,它只需要相关的参数就可以。所以普通的在module中定义的称谓函数是很有道理的。
那么方法的意思就很明确了,它是与某个对象相互关联的,也就是说它的实现与某个对象有关联关系。这就是方法。虽然它的定义方式和函数是一样的。也就是说,在Class定义的函数就是方法。本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 1无用