方法和函数之间的区别 [英] Difference between a method and a function

查看:139
本文介绍了方法和函数之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以在OOP上下文中提供方法函数的简单解释吗?

Can someone provide a simple explanation of methods vs. functions in OOP context?

推荐答案

功能是一段按名称调用的代码。它可以传递数据来操作(即参数),并可以选择返回数据(返回值)。所有传递给函数的数据都被明确​​地传递。

A 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.

一个方法是一段代码,与一个对象。在大多数方面,除了两个关键的区别之外,它与函数是相同的:

A 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. 方法隐式传递调用它的对象。

  2. 一个方法可以对类中包含的数据进行操作(记住一个对象是一个类的实例 - 类是定义,对象是该类的一个实例数据)。
  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.)

这篇关于方法和函数之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆