函数是一段代码,通过名字来进行调用。它能将一些数据(参数)传递进去进行处理,然后返回一些数据(返回值),也可以没有返回值。

所有传递给函数的数据都是显式传递的。

 

方法也是一段代码,通过一个与对象相关联的名字来进行调用。方法和函数大致上是相同的,但有两个主要的不同之处:

  1. 方法中的数据是隐式传递的;
  2. 方法可以操作类内部的数据(请记住,对象是类的实例化–类定义了一个数据类型,而对象是该数据类型的一个实例化)
  3. 方法最终通过转化为函数的形式进行调用。

 

function is a piece of code that is called by name. It can be passed data to operate on (i.e. the parameters) and can optionally return data (the return value). All data that is passed to a function is explicitly passed.

method is a piece of code that is called by a name that is associated with an object. In most respects it is identical to a function except for two key differences:

  1. A method is implicitly passed the object on which it was called.
  2. A method is able to operate on data that is contained within the class (remembering that an object is an instance of a class - the class is the definition, the object is an instance of that data).

(this is a simplified explanation, ignoring issues of scope etc.)

 

方法通过关联到一个对象(类型)上的名称进行调用。

函数直接通过名称调用。

 

方法和对象相关;

函数和对象无关。

Java中只有方法,C中只有函数,而C++里取决于是否在类中。

 

Simple way to remember:

  • Function → Free (Free means not belong to an object or class)
  • Method → Member (A member of an object or class)

 

http://blog.jobbole.com/44230/

https://stackoverflow.com/questions/155609/whats-the-difference-between-a-method-and-a-function/155655#155655


------------------越是喧嚣的世界,越需要宁静的思考------------------ 合抱之木,生于毫末;九层之台,起于垒土;千里之行,始于足下。 积土成山,风雨兴焉;积水成渊,蛟龙生焉;积善成德,而神明自得,圣心备焉。故不积跬步,无以至千里;不积小流,无以成江海。骐骥一跃,不能十步;驽马十驾,功在不舍。锲而舍之,朽木不折;锲而不舍,金石可镂。蚓无爪牙之利,筋骨之强,上食埃土,下饮黄泉,用心一也。蟹六跪而二螯,非蛇鳝之穴无可寄托者,用心躁也。